Vector push_back() in C++ STL Last Updated : 27 Dec, 2024 Comments Improve Suggest changes Like Article Like Report 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: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 4, 6}; // Add an element at the end of the vector v.push_back(9); for (int i : v) cout << i << " "; return 0; } Output1 4 6 9 This article covers the syntax, usage, and common examples of the vector push_back() method in C++:Table of ContentSyntax of Vector push_back()Examples of vector push_back()Initialize an Empty Vector One by OneAdd Elements Vector of Strings at the EndSyntax 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 C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Add elements to the vector v.push_back(3); v.push_back(7); v.push_back(9); for (int i : v) cout << i << " "; return 0; } Output3 7 9 Add Elements Vector of Strings at the End C++ #include <bits/stdc++.h> using namespace std; int main() { vector<string> v = {"Hello", "Geeks"}; // Add string to the vector v.push_back("Welcome"); for (string s: v) cout << s << " "; return 0; } OutputHello Geeks Welcome Comment More infoAdvertise with us A AyushSaxena Follow Improve Article Tags : Misc C++ STL CPP-Library CPP-Functions cpp-vector +2 More Explore Introduction to C++Introduction to C++ Programming Language2 min readHeader Files in C++5 min readSetting up C++ Development Environment8 min readDifference between C and C++3 min readBasicsC++ Data Types7 min readC++ Variables4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readC++ Loops7 min readFunctions in C++8 min readC++ Arrays8 min readStrings in C++5 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readC++ OOPInheritance in C++10 min readC++ Polymorphism5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Containers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice C++C++ Interview Questions and Answers1 min readTop C++ DSA Related ProblemsC++ Programming Examples7 min read Like