forward_list::push_front() and forward_list::pop_front() in C++ STL - GeeksforGeeks

06 Oct, 2021
Forward list in STL implements singly linked list. Introduced from C++11, forward list are useful than other containers in insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from list by the fact that forward list keeps track of location of only next element while list keeps track to both next and previous elements. 
push_front() function is used to push elements into a Forward list from the front. The new value is inserted into the Forward list at the beginning, before the current first element and the container size is increased by 1.Syntax :  
forwardlistname.push_front(value)
Parameters :
The value to be added in the front is 
passed as the parameter
Result :
Adds the value mentioned as the parameter to the
front of the forward list named as forwardlistname
Examples:  
Input : forward_list forwardlist{1, 2, 3, 4, 5};
        forwardlist.push_front(6);
Output : 6, 1, 2, 3, 4, 5

Input : forward_list forwardlist{5, 4, 3, 2, 1};
        forwardlist.push_front(6);
Output :6, 5, 4, 3, 2, 1
Errors and Exceptions1. Strong exception guarantee – if an exception is thrown, there are no changes in the container. 2. If the value passed as argument is not supported by the forward list, it shows undefined behavior. 
CPP
// CPP program to illustrate// push_front() function#include <forward_list>#include <iostream>using namespace std; int main(){    forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 };    myforwardlist.push_front(6);     // Forward list becomes 6, 1, 2, 3, 4, 5     for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)        cout << ' ' << *it;}
Output:  
6 1 2 3 4 5
Application : Input an empty forward list with the following numbers and order using push_front() function and sort the given forward list.  
Input :  7, 89, 45, 6, 24, 58, 43
Output : 6, 7, 24, 43, 45, 58, 89
 
CPP
// CPP program to illustrate// application Of push_front() function#include <forward_list>#include <iostream>using namespace std; int main(){    forward_list<int> myforwardlist{};    myforwardlist.push_front(43);    myforwardlist.push_front(58);    myforwardlist.push_front(24);    myforwardlist.push_front(6);    myforwardlist.push_front(45);    myforwardlist.push_front(89);    myforwardlist.push_front(7);     // Forward list becomes 7, 89, 45, 6, 24, 58, 43    // Sorting function     myforwardlist.sort();     for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)        cout << ' ' << *it;}
Output  
6 7 24 43 45 58 89
 
pop_front() function is used to pop or remove elements from a forward list from the front. The value is removed from the list from the beginning, and the container size is decreased by 1.Syntax :  
forwardlistname.pop_front()
Parameters :
No parameter is passed as the parameter.
Result :
Removes the value present at the front 
of the given forward list named as forwardlistname
Examples:  
Input : forward_list forwardlist{1, 2, 3, 4, 5};
        forwardlist.pop_front();
Output :2, 3, 4, 5

Input : forward_list forwardlist{5, 4, 3, 2, 1};
        forwardlist.pop_front();
Output :4, 3, 2, 1
Errors and Exceptions1. No-Throw-Guarantee – if an exception is thrown, there are no changes in the container. 2. If the list is empty, it shows undefined behaviour. 
CPP
// CPP program to illustrate// pop_front() function#include <forward_list>#include <iostream>using namespace std; int main(){    forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 };    myforwardlist.pop_front();     // forward list becomes 2, 3, 4, 5     for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it)        cout << ' ' << *it;}
Output:  
2 3 4 5
Application : Input an empty forward 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
// CPP program to illustrate// application Of pop_front() function#include <forward_list>#include <iostream>using namespace std; int main(){    forward_list<int> myforwardlist{}, newforwardlist{};    myforwardlist.push_front(8);    myforwardlist.push_front(7);    myforwardlist.push_front(6);    myforwardlist.push_front(5);    myforwardlist.push_front(4);    myforwardlist.push_front(3);    myforwardlist.push_front(2);    myforwardlist.push_front(1);     // Forward list becomes 1, 2, 3, 4, 5, 6, 7, 8     while (!myforwardlist.empty()) {        newforwardlist.push_front(myforwardlist.front());        myforwardlist.pop_front();    }    for (auto it = newforwardlist.begin(); it != newforwardlist.end(); ++it)        cout << ' ' << *it;}
Output  
8 7 6 5 4 3 2 1
 
hritikbhatnagar2182
cpp-containers-library
CPP-forward-list
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
Bitwise Operators in C/C++
Operator Overloading in C++
Constructors in C++
Socket Programming in C/C++
Virtual Function in C++
Multidimensional Arrays in C / C++
Templates in C++ with Examples