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

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of forward_list::push_front() and forward_list::pop_front() functions in C++.

What is a Forward_list in STL?

Forward list are sequence containers that allow constant time insert and erase operations anywhere within the sequence. Forward list are implement as a singly-linked lists. The ordering is kept by the association to each element of a link to the next element in the sequence.

What is forward_list::push_front()?

forward_list::push_front() is an inbuilt function in C++ STL which is declared in
header file. push_front() is used to push/insert an element or a value in the front or at the
beginnig of the forward_list. When we use this function the first element which is already in the
container becomes the second, and the element pushed becomes the first element of the
forward_list container and the size of the container is increased by 1.

Syntax

flist_container1.push_front (const value_type& value );

This function can accept only one parameter, i.e. the value which is to be inserted at the beginning.

Return Value

This function returns nothing.

push_front()

Example

In the below code we are inserting element to the front of a list using push_front() operation and after that we are using sort() function to sort the list elements.

 Live Demo

#include <forward_list>
#include <iostream>
using namespace std;
int main(){
   forward_list<int> forwardList = {12, 21, 22, 24};
   //inserting element in the front of a list using push_front() function
   forwardList.push_front(78);
   cout<<"Forward List contains: ";
   for (auto i = forwardList.begin(); i != forwardList.end(); ++i)
      cout << ' ' << *i;
   //list after applying sort operation
   forwardList.sort();
   cout<<"\nForward List after performing sort operation : ";
   for (auto i = forwardList.begin(); i != forwardList.end(); ++i)
      cout << ' ' << *i;
}

Output

If we run the above code it will generate the following output

Forward List contains: 78 12 21 22 24
Forward List after performing sort operation : 12 21 22 24 78

What is forward_list::pop_front()?

forward_list::pop_front() is an inbuilt function in C++ STL which is declared in <forward_list>header file. pop_front() is used to pop/removes the element which is in the the beginnig of the forward_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 forward_list container and the size of the container is decreased by 1.

Syntax

flist_container1.pop_front ();

This function accepts no parameter

Return Value

This function returns nothing.

pop_front()

Example

In the below code we are removing the first element of a list using pop_front() operation presen in a C++ STL.

 Live Demo

#include <forward_list>
#include <iostream>
using namespace std;
int main(){
   forward_list<int> forwardList = {10, 20, 30 };
   //List before applying pop operation
   cout<<"list before applying pop operation : ";
   for(auto i = forwardList.begin(); i != forwardList.end(); ++i)
      cout << ' ' << *i;
   //List after applying pop operation
   cout<<"\nlist after applying pop operation : ";
   forwardList.pop_front();
   for (auto j = forwardList.begin(); j != forwardList.end(); ++j)
      cout << ' ' << *j;
}

Output

If we run the above code it will generate the following output

list before applying pop operation : 10 20 30
list after applying pop operation : 20 30
raja
Published on 02-Mar-2020 12:46:47
Advertisements