C++ Standard Library C++ STL Library

C++ <vector> - pop_back() Function



The C++ vector::pop_back function is used to delete the last element of the vector. Every deletion of element results into reducing the container size by one unless the vector is empty.

Syntax

void pop_back();
void pop_back();

Parameters

No parameter is required.

Return Value

None.

Time Complexity

Constant i.e, Θ(1).

Example:

In the example below, the vector::pop_back function is used to delete last elements of the vector called MyVector.

#include <iostream>
#include <vector>
using namespace std;
 
int main (){
  vector<int> MyVector{100, 200, 300, 400, 500, 600};
  vector<int>::iterator it;

  //deletes last element of the vector
  MyVector.pop_back();
  //deletes next last element of the vector
  MyVector.pop_back();

  cout<<"The vector contains:";
  for(it = MyVector.begin(); it != MyVector.end(); ++it)
    cout<<" "<<*it;

  return 0;
}

The output of the above code will be:

The vector contains: 100 200 300 400

❮ C++ <vector> Library