
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
list::pop_front() and list::pop_back() in C++ STL
In this article we will be discussing the working, syntax and examples of list::pop_front() and list::pop_back() functions in C++ STL.
What is a List in STL?
List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List performs better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.
What is forward_list::pop_front()?
list::pop_front() is an inbuilt function in C++ STL which is declared in header f ile. pop_front() is used to pop/remove the element which is in the beginning of the list. When we use this function the first element which is already in the container is removed and the next element to the first element becomes the first element of the list container and the size of the container is decreased by 1.
Syntax
list_container1.pop_front ();
Parameters
This function accepts no parameters.
Return Value
This function returns nothing.
Example
Input: list<int> List_container= {10, 11, 13, 15}; List_container.pop_front(); Output: List = 11 13 15
Example
#include <iostream> #include <list> using namespace std; int main(){ list<int> myList_1 = {}, myList_2 = {}; myList_1.push_front(10); myList_1.push_front(20); myList_1.push_front(30); myList_1.push_front(40); myList_1.push_front(50); while (!myList_1.empty()){ myList_2.push_front(myList_1.front()); myList_1.pop_front(); } cout<<"Elements in the list are : "; for (auto i = myList_2.begin(); i!= myList_2.end(); ++i) cout << ' ' << *i; }
Output
If we run the above code it will generate the following output −
Elements in the list are : 10 20 30 40 50
What is list::pop_back()?
list::pop_back() is an inbuilt function in C++ STL which is declared in header file. pop_back() is used to remove/pop the element from the back or the last of the list container. When we use pop_back then it removes/pops the last element and the element before the last element becomes the last element and the size of the list container is reduced by 1.
Syntax
list_container.pop_back();
Parameter
This function accepts no parameters.
Return Value
This function returns nothing.
Example
Input: list<int> List_container= {10, 11, 13, 15}; List_container.pop_back(); Output: List = 10 11 13
Example
#include <iostream> #include <list> using namespace std; int main(){ list<int> myList_1 = {}, myList_2 = {}; myList_1.push_front(10); myList_1.push_front(20); myList_1.push_front(30); myList_1.push_front(40); myList_1.push_front(50); while (!myList_1.empty()){ myList_2.push_front(myList_1.back()); myList_1.pop_back(); } cout<<"Elements in the list are : "; for (auto i = myList_2.begin(); i!= myList_2.end(); ++i) cout << ' ' << *i; }
Output
If we run the above code it will generate the following output −
Elements in the list are : 50 40 30 20 10
- Related Articles
- list::front() and list::back() in C++ STL
- stack push() and pop() in C++ STL
- queue::push() and queue::pop() in C++ STL
- list front() function in C++ STL
- list back() function in C++ STL
- Program to implement a queue that can push or pop from the front, middle, and back in Python
- queue::front() and queue::back() in C++ STL
- How to pop the first element from a C# List?
- deque front( ) and deque back( ) in C++ in STL
- Difference between OOP and POP
- Program to convert linked list by alternating nodes from front and back in Python
- list begin( ) and list end( ) in C++ STL
- list::emplace_front() and list::emplace_back() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- Difference between shift() and pop() methods in Javascript
