list::pop_front() and list::pop_back() in C++ STL - GeeksforGeeks

12 Dec, 2017
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists.
pop_front() function is used to pop or remove elements from a list from the front. The value is removed from the list from the beginning, and the container size is decreased by 1.
Syntax :
listname.pop_front()
Parameters :
No argument is passed as parameter.
Result :
Removes the value present at the front 
of the given list named as listname

Examples:
Input :  list list{1, 2, 3, 4, 5};
         list.pop_front();
Output : 2, 3, 4, 5

Input :  list list{5, 4, 3, 2, 1};
         list.pop_front();
Output : 4, 3, 2, 1

Errors and Exceptions
No-Throw-Guarantee – if an exception is thrown, there are no changes in the container.If the list is empty, it shows undefined behaviour.
No-Throw-Guarantee – if an exception is thrown, there are no changes in the container.
If the list is empty, it shows undefined behaviour.
// CPP program to illustrate// pop_front() function#include <iostream>#include <list>using namespace std;  int main(){    list<int> mylist{ 1, 2, 3, 4, 5 };    mylist.pop_front();      // list becomes 2, 3, 4, 5      for (auto it = mylist.begin(); it != mylist.end(); ++it)        cout << ' ' << *it;}
Output:
2, 3, 4, 5

Application : Input an empty list with the following numbers and order using push_front() function and print the reverse of the list.
Input : 1, 2, 3, 4, 5, 6, 7, 8
Output: 8, 7, 6, 5, 4, 3, 2, 1

// CPP program to illustrate// application Of pop_front() function#include <iostream>#include <list>using namespace std;  int main(){    list<int> mylist{}, newlist{};    mylist.push_front(8);    mylist.push_front(7);    mylist.push_front(6);    mylist.push_front(5);    mylist.push_front(4);    mylist.push_front(3);    mylist.push_front(2);    mylist.push_front(1);      // list becomes 1, 2, 3, 4, 5, 6, 7, 8      while (!mylist.empty()) {        newlist.push_front(mylist.front());        mylist.pop_front();    }    for (auto it = newlist.begin(); it != newlist.end(); ++it)        cout << ' ' << *it;}
Output:
8, 7, 6, 5, 4, 3, 2, 1

pop_back() function is used to pop or remove elements from a list from the back. The value is removed from the list from the end, and the container size is decreased by 1.
Syntax :
listname.pop_back()
Parameters :
No argument is passed as parameter.
Result :
Removes the value present at the end or back 
of the given list named as listname

Examples:
Input :  list list{1, 2, 3, 4, 5};
         list.pop_back();
Output : 1, 2, 3, 4

Input :  list list{5, 4, 3, 2, 1};
         list.pop_back();
Output : 5, 4, 3, 2

Errors and Exceptions
No-Throw-Guarantee – if an exception is thrown, there are no changes in the container.If the list is empty, it shows undefined behaviour.
No-Throw-Guarantee – if an exception is thrown, there are no changes in the container.
If the list is empty, it shows undefined behaviour.
// CPP program to illustrate// pop_back() function#include <iostream>#include <list>using namespace std;  int main(){    list<int> mylist{ 1, 2, 3, 4, 5 };    mylist.pop_back();      // list becomes 1, 2, 3, 4      for (auto it = mylist.begin(); it != mylist.end(); ++it)        cout << ' ' << *it;}
Output:
1, 2, 3, 4

Application : Input an empty list with the following numbers and order using push_front() function and print the reverse of the list.
Input : 1, 20, 39, 43, 57, 64, 73, 82
Output: 82, 73, 64, 57, 43, 39, 20, 1

// CPP program to illustrate// application Of pop_back() function#include <iostream>#include <list>using namespace std;  int main(){    list<int> mylist{}, newlist{};    mylist.push_front(82);    mylist.push_front(73);    mylist.push_front(64);    mylist.push_front(57);    mylist.push_front(43);    mylist.push_front(39);    mylist.push_front(20);    mylist.push_front(1);      // list becomes 1, 20, 39, 43, 57, 64, 73, 82      while (!mylist.empty()) {        newlist.push_back(mylist.back());        mylist.pop_back();    }    for (auto it = newlist.begin(); it != newlist.end(); ++it)        cout << ' ' << *it;}
Output:
82, 73, 64, 57, 43, 39, 20, 1

CPP-Library
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Operator Overloading in C++
Socket Programming in C/C++
Bitwise Operators in C/C++
Multidimensional Arrays in C / C++
Virtual Function in C++
Constructors in C++
Templates in C++ with Examples