Vector pop_back() in C++ STL Last Updated : 26 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the vector pop_back() is a built-in method used to remove the last element from a vector. It reduces the size of the vector by one, but the capacity remains unchanged.Let’s take a look at an example that illustrates the vector pop_back() method: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {1, 4, 6, 9}; // Remove the last element v.pop_back(); for (int i : v) cout << i << " "; return 0; } Output1 4 6 This article covers the syntax, usage, and common examples of the vector pop_back() method in C++:Table of ContentSyntax of Vector pop_back()Examples of Vector pop_back()Remove Last Element from a VectorUse pop_back() on an Empty VectorDelete All Elements from a VectorSyntax of Vector pop_back()The vector pop_back() is a member method of the std::vector class defined inside the <vector> header file.v.pop_back();This function neither takes any parameter nor returns any value.Examples of Vector pop_back()The following examples demonstrate the use of the vector pop_back() function for different purposes:Remove Last Element from a Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {3, 7, 9}; // Remove the last element v.pop_back(); for (int i : v) cout << i << " "; return 0; } Output3 7 Use pop_back() on an Empty Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Try to remove element from empty vector if (!v.empty()) v.pop_back(); else cout << "Vector is empty!"; return 0; } OutputVector is empty!Explanation: Before calling pop_back(), it is a good practice to check if the vector is empty using the vector empty() method, as pop_back() on an empty vector leads to undefined behavior.Delete All Elements from a Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {3, 7, 9}; // Remove elements till vector is empty while(!v.empty()) v.pop_back(); cout << v.size(); return 0; } Output0Explanation: Using the while loop, an element is removed from the end of the vector till the vector becomes empty. Comment More infoAdvertise with us Next Article Vector pop_back() in C++ STL A abhishekcpp Follow Improve Article Tags : C++ STL CPP-Functions cpp-vector cpp-containers-library +1 More Practice Tags : CPPSTL Similar Reads Vector back() in C++ STL In C++, the vector back() is a built-in function used to retrieve the last element of the vector. It provides a reference to the last element which allows us to read or modify it directly.Letâs take a quick look at a simple example that illustrates the vector back() method:C++#include <bits/stdc+ 2 min read Vector push_back() in C++ STL 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/ 2 min read vector::at() in C++ STL In C++, vector at() is a built-in method used to access an element in a vector using index. It is the only access method that performs bound checking before accessing the element to confirm whether the given index lies is within the vector.Letâs take a quick look at a simple example that uses vector 2 min read Array of Vectors in C++ STL Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam 3 min read Vector emplace_back in C++ STL In C++, the vector emplace_back() is a built-in method used to insert an element at the end of the vector by constructing it in-place. It means that the element is created directly in the memory allocated to the vector avoiding unnecessary copies or moves.Letâs take a quick look at a simple example 3 min read Like