In this C++ tutorial, you will learn how to remove the last element from a vector using vector::pop_back() function, with example program.

C++ Vector pop_back() function

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

pop_back() reduces the size of vector by one.

Examples

1. Remove last element of vector

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.