In C++, the standard library provides us with vectors, which are dynamic arrays. It consists of multiple homogeneous objects, which can be accessed by their position in the vector. It is automatically resized (if needed). The vector::push_back(val)
is a C++ Standard Library function which pushes/inserts the element val at the end of a vector. It effectively increases the size of vector by 1.
Syntax:
vector_name.push_back(value);
Parameter: The vector::push_back()
accepts a single parameter:
The value that has to be added to the end of the vector
Return value: none
Example of vector::push_back() function
#include <iostream> #include <vector> using namespace std; int main () { vector<int> vec; for(int i=1;i<=5;i++) vec.push_back(i); cout<<"Vector after pushing values: "; for(int i=0;i<5;i++) cout<<vec[i]<<" "; }
Output:
Vector after pushing values: 1 2 3 4 5