C++ List::pop_back() Function



The C++ std::list::pop_back() function is used to remove the last element of the list. It does not accept any parameter, removes(or pops) the last element of the current list, and reduces the list size by one.This function does not return any value because the function return type is void.

Syntax

Following is the syntax of the C++ std::list::void pop_back() function −

void pop_back();

Parameters

  • It does not accept any parameter.

Return Value

This function does not return any value.

Example 1

In the following program, we are using the C++ std::list::pop_back() function to remove( or pop) the last element 60 of the current list {10, 20, 30, 40, 50, 60}.

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

int main() {
   list<int> lst = {10, 20, 30, 40, 50, 60};
   cout<<"The list elements before the pop_back() operation: "<<endl;
   for(int l : lst) {
      cout<<l<<" ";
   }
   lst.pop_back();
   cout<<"\nThe list elements after the pop_back() operation: ";
   for(int l1 : lst) {
      cout<<l1<<" ";
   }
}

Output

on executing the above program, it will produce the following output −

The list elements before the pop_back() operation: 
10 20 30 40 50 60 
The list elements after the pop_back() operation: 10 20 30 40 50 

Example 2

Apart from the integer element, you can also remove the last char element from the list(type char).

Following is another example of the C++ std::list::pop_back() function. Here, we are creating a list(type char) with the elements {'+', '-', '$', '#', '@'}. Then, using the pop_back() function, we are trying to remove the last element '@' from this list.

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

int main() {
   list<char> lst = {'+', '-', '$', '#', '@'};
   cout<<"The list elements before the pop_back() operation: "<<endl;
   for(char l : lst) {
      cout<<l<<" ";
   }
   lst.pop_back();
   cout<<"\nThe list elements after the pop_back() operation: ";
   for(char l1 : lst) {
      cout<<l1<<" ";
   }
}

Output

Following is the output of the above program −

The list elements before the pop_back() operation: 
+ - $ # @ 
The list elements after the pop_back() operation: + - $ #

Example 3

You can also remove the last string from the list(type string).

In this example, we are creating a list(type string) named colors with the values {"Red", "Green", "Yellow", "Green", "Blue"}. Then, using the std::list::pop_back() function, we are trying to remove the last element "Blue" from this list.

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

int main() {
   list<string> colors = {"Red", "Green", "Yellow", "Green", "Blue"};
   cout<<"The list elements before the pop_back() operation: "<<endl;
   for(string l : colors) {
      cout<<l<<" ";
   }
   colors.pop_back();
   cout<<"\nThe list elements after the pop_back() operation: ";
   for(string l1 : colors) {
      cout<<l1<<" ";
   }
}

Output

The above program generates the following output −

The list elements before the pop_back() operation: 
Red Green Yellow Green Blue 
The list elements after the pop_back() operation: Red Green Yellow Green

Example 4

If the list is empty and contains white spaces, the pop_back() function removes the white spaces from the list and reduces the size of the list by one.

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

int main() {
   list<string> colors = {"      "};
   cout<<"The list elements before the pop_back() operation: "<<endl;
   cout<<"List size before the pop_back() operation: "<<colors.size()<<endl;
   for(string l : colors) {
      cout<<l<<" ";
   }
   colors.pop_back();
   cout<<"\nThe list elements after the pop_back() operation: ";
   for(string l1 : colors) {
      cout<<l1<<" ";
   }
   cout<<"\nList size after the pop_back() operation: "<<colors.size();
}

Output

After executing the above program, it produces the following output −

The list elements before the pop_back() operation: 
List size before the pop_back() operation: 1
The list elements after the pop_bak() operation: 
List size after the pop_back() operation: 0
Advertisements