VOOZH about

URL: https://www.geeksforgeeks.org/cpp/vector-push-back-cpp-stl/

⇱ Vector push_back() in C++ STL - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Vector push_back() in C++ STL

Last Updated : 27 Dec, 2024

In C++, the vector push_back() is a built-in method used to add a new element at the end of the vector. It automatically resizes the vector if there is not enough space to accommodate the new element.

Let’s take a look at an example that illustrates the vector push_back() method:


Output
1 4 6 9 

This article covers the syntax, usage, and common examples of the vector push_back() method in C++:

Syntax of Vector push_back()

The vector push_back() is a member method of the std::vector class defined inside the <vector> header file.

v.push_back(val);

Parameters:

  • val: Element to be added to the vector at the end.

Return Value:

  • This function does not return any value.

Examples of vector push_back()

The following examples demonstrate the use of the vector push_back() function for different purposes:

Initialize an Empty Vector One by One


Output
3 7 9 

Add Elements Vector of Strings at the End


Output
Hello Geeks Welcome 
Comment