C++ Standard Library C++ STL Library

C++ <list> - pop_back() Function



The C++ list::pop_back function is used to delete the last element of the list. Every deletion of element results into reducing the container size by one unless the list 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 list::pop_back function is used to delete last elements of the list called MyList.

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

  //deletes last element of the list
  MyList.pop_back();
  //deletes next last element of the list
  MyList.pop_back();

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

  return 0;
}

The output of the above code will be:

MyList contains: 100 200 300 400

❮ C++ <list> Library