C++

Vector Push_Back() Function in C++

The dynamic array can be implemented by using a vector in C++. The elements can be added to the vector in different ways. The push_back() function is one of the ways to insert a new element at the end of the vector that increases the size of the vector by 1. This function is useful when one element is required to add to the vector.  If the data type of the vector does not support the value passed by the argument of this function, then an exception will be generated, and no data will be inserted. The way to insert data in vector using the push_back() function has shown in this tutorial.

Syntax:

vector::push_back(value_type n);

The value of the n will be inserted at the end of the vector if the data type of the vector supports the data type of the n. It returns nothing.

Pre-requisite:

Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code. Different uses of the push_back() function to insert element(s) into a vector have shown in the next part of this tutorial.

Example-1: Adding multiple elements at the end of the vector

Create a C++ file with the following code to insert multiple elements at the end of the vector using the push_back() function. A vector of three string values has been defined in the code. The push_back() function has been called three times to insert three elements at the end of the vector. The content of the vector will be printed before and after inserting the elements.

//Include necessary libraries

#include <iostream>

#include <vector>

using namespace std;

int main()

{

        //Declare a vector of string values

        vector<string> birds = {"Gray Parrot", "Diamond Dove", "Cocktail"};

        cout << "The values of the vector before insert:\n";

        //Iterate the vector using loop to print the values

        for(int i = 0; i < birds.size(); ++i)

                cout << birds[i] << " ";

        cout << "\n";

        /*

        Add three values at the end of the vectior

        using push_back() function

        */


        birds.push_back("Mayna");

        birds.push_back("Budgies");

        birds.push_back("Cockatoo");

        cout << "The values of the vector after insert:\n";

        //Iterate the vector using loop to print the values

        for(int i = 0; i < birds.size(); ++i)

                cout << birds[i] << " ";

        cout << "\n";

        return 0;

}

Output:

The following output will appear after executing the above code. The output shows that three new elements have been inserted at the end of the vector.

Example-2: Insert values into the vector by input

Create a C++ file with the following code to insert the element into an empty vector by taking values from the user and using the push_back() function. An empty vector of integer data type has been declared in the code. Next, a ‘for’ loop takes 5 numbers from the user and inserts the numbers into the vector using the push_back() function.  The content of the vector will be printed after the insertion.

//Include necessary libraries

#include <iostream>

#include <vector>

using namespace std;

int main ()

{

        //Declare an integer vector

        vector<int> intVector;

        //Declare an integer number

        int number;

        cout << "Enter 5 numbers: \n";

        /*

        Iterate the loop for 5 times to insert 5 integer values

        into the vector using push_back() function

        */

       
        for( int i=0; i < 5; i++) {

                cin >> number;

                intVector.push_back (number);

        }

        cout << "The values of the vector after insert:\n";

        //Iterate the vector using loop to print the values

        for(int i = 0; i < intVector.size(); ++i)

                cout << intVector[i] << " ";

        cout << "\n";

        return 0;

}

Output:

The following output will appear after executing the above code. The output shows that the five numbers taken from the user have been inserted into the vector.

Example-3: Insert values into vector-based on the specific condition

Create a C++ file with the following code to insert the specific numbers from an integer array into an empty vector. An empty vector and an array of 10 integer numbers have been declared in the code. The ‘for’ loop has been used to iterate each value of the array and insert the number into the vector using the push_back() function if the number is less than 30 or greater than 60. The content of the vector will be printed using the display_vector() function after the insertion.

//Include necessary libraries

#include <iostream>

#include <vector>

using namespace std;

//Display the vector

void display_vector(vector<int> nums)

{

        //Print the values of the vector using loop

        for(auto ele = nums.begin(); ele != nums.end() ; ele++)

                cout << *ele << " ";

        //Add new line

        cout << "\n";

}

int main ()

{

        //Declare an integer vector

        vector<int> intVector;

        //Declare an array of numbers

        int myArray[10] = { 9, 45, 13, 19, 30, 82, 71, 50, 35, 42 };

        /*

        Iterate the loop to read each element of the array

        and insert those values into the vector

        which are less than 30 and greater than 60

        using push_back() function

        */


        for (int i=0; i < 10; i++) {

                if(myArray[i] < 30 || myArray[i] > 60)

                intVector.push_back (myArray[i]);

        }

        cout << "The values of the vector after insert: " << endl;

        display_vector(intVector);

        return 0;

}

Output:

The following output will appear after executing the above code. The output shows that the numbers 9, 13, 19,  82, and 71 have been inserted into the vector.

Conclusion:

Many functions exist in C++ to insert data at the beginning or ending or any particular position of the vector, such as push_front(), insert(), etc. Using the push_back() function will be cleared after practicing the examples shown in this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.