C++ Vector pop_back() function

pop_back() function removes the last element of the vector.

pop_back() reduces the size of vector by one.

Example 1: Vector pop_back()

In the following C++ program, we define a vector of integers, and added some elements to it. Then we shall call pop_back() function on the vector and remove the last element.

C++ Program

#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> nums;
   nums.push_back(24);
   nums.push_back(81);
   nums.push_back(57);

   nums.pop_back();

   for (int element: nums)
      cout << element << endl;
}

Output

24
81

Conclusion

In this C++ Tutorial, we learned how to remove the last element of a vector using pop_back() function.