C++ List::pop_front() Function



The C++ std::list::pop_front() function is used to remove the first element of the list. It does not accept any parameter, removes(or pops) the first element of the current list, and reduces the list size by one. The pop_front() 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_front() function −

void pop_front();

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_front() function to remove( or pop) the first element 1 of the current list {1, 2, 3, 4, 5, 6}.

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

int main() {
   list<int> lst = {1, 2, 3, 4, 5, 6};
   cout<<"The list elements before the pop_front() operation: "<<endl;
   for(int l : lst) {
      cout<<l<<" ";
   }
   lst.pop_front();
   cout<<"\nThe list elements after the pop_front() operation: ";
   for(int l1 : lst) {
      cout<<l1<<" ";
   }
}

Output

Following is the output of the above program −

The list elements before the pop_front() operation: 
1 2 3 4 5 6 
The list elements after the pop_front() operation: 2 3 4 5 6

Example 2

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

Following is another example of the C++ std::list::pop_front() function. Here, we are creating a list(type char) with the elements {'A', 'B', 'C', 'D', 'E'}. Then, using the pop_front() function, we are trying to remove the first element 'A' from this list.

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

int main() {
   list<char> lst = {'A', 'B', 'C', 'D', 'E'};
   cout<<"The list elements before the pop_front() operation: "<<endl;
   for(char l : lst) {
      cout<<l<<" ";
   }
   lst.pop_front();
   cout<<"\nThe list elements after the pop_front() operation: ";
   for(char l1 : lst) {
      cout<<l1<<" ";
   }
}

Output

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

The list elements before the pop_front() operation: 
A B C D E 
The list elements after the pop_front() operation: B C D E 

Example 3

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

In this example, we are creating a list(type string) named names with the values {"Rohan", "Geeta", "Rahul", "Sonu", "Vinod"}. Then, using the std::list::pop_front() function, we are trying to remove the fist element "Rohan" from this list.

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

int main() {
   list<string> names = {"Rohan", "Geeta", "Rahul", "Sonu", "Vinod"};
   cout<<"The list elements before the pop_front() operation: "<<endl;
   for(string l : names) {
      cout<<l<<" ";
   }
   names.pop_front();
   cout<<"\nThe list elements after the pop_front() operation: ";
   for(string l1 : names) {
      cout<<l1<<" ";
   }
}

Output

The above program generates the following output −

The list elements before the pop_front() operation: 
Rohan Geeta Rahul Sonu Vinod 
The list elements after the pop_front() operation: Geeta Rahul Sonu Vinod

Example 4

If the list is empty and contains white spaces, the pop_front() 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_front() operation: "<<endl;
   cout<<"List size before the pop_front() operation: "<<colors.size()<<endl;
   for(string l : colors) {
      cout<<l<<" ";
   }
   colors.pop_front();
   cout<<"\nThe list elements after the pop_front() operation: ";
   for(string l1 : colors) {
      cout<<l1<<" ";
   }
   cout<<"\nList size after the pop_front() operation: "<<colors.size();
}

Output

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

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