list::pop_front() method

Lists are sequence containers available in the C++ Standard Library. They allow non-contiguous memory allocation. List containers are implemented as doubly-linked lists. Compared to other sequence containers (eg: arrays, vectors etc.), lists have a better performance when it comes to inserting, moving or deleting elements. The list::pop_front() is a method available in the STL which deletes the first element of the list. It effectively decreases the container size by one.

Syntax:

list_name.pop_front();

Parameters: The list::pop_front() does not accept any parameter:
Return value: none

Example of list::pop_front() method

#include <iostream>
#include <list>

using namespace std;

int main()
{
    list<int> L;
    L.push_back(5);
    L.push_back(10);
    L.push_back(15);
    L.push_back(20);

    cout<<"List before pop:  \n";

    for( list<int>::iterator it = L.begin(); it != L.end(); ++it)
    {
        cout<<*it<<" <--> ";
    }
    cout<<"NULL";
    cout<<endl;
    L.pop_front();

    cout<<"List after pop:  \n";

    for( list<int>::iterator it = L.begin(); it != L.end(); ++it)
    {
        cout<<*it<<" <--> ";
    }
    cout<<"NULL";
    return 0;
}

Output:

List before pop:  
5 <--> 10 <--> 15 <--> 20 <--> NULL
List after pop:  
10 <--> 15 <--> 20 <--> NULL

This article is written by

Please comment below, if you have any doubts or find any error in the above article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top