C++ vector::push_back() usage with example

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.



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++)

  cout<<"Vector after pushing values: ";
  for(int i=0;i<5;i++)
    cout<<vec[i]<<"  ";



Vector after pushing values: 1  2  3  4  5

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]