Forward_list in STL is a sequential container. It is very similar to list in STL but the list stores both the location of the previous and the next elements of the list while forward_list only stores the location of the next element, thus it saves memory. Insertion and deletion are efficient in forward_list and take only constant time. It is implemented as a singly LinkedList in memory. Forward_lists are included in #include <forward_list> header file.
Forward_list Declaration
Syntax:
forward_list<data_type> name;
data_type: data type of the elements to be stored in the list.
Example:
forward_list<int> l; //initializes a forward list of size 0 which stores integer values Forward_list Initialization
forward_list<data_type> name={initial_values};
initial_values: optional parameter which initializes the list with the given values.
Example:
forward_list<int> l = {1,2,3,4,5}; //initializes a forward list of size 5 with values 1,2,3,4,5 Functions on forward_list
push_front(element): It adds a new element to the front of the list.
Parameters: the element to be inserted
Return value: void
pop_front(): It deletes the first element of the list.
Parameters: None
Return value: void
insert_after(iterator,{values}): It inserts the values after the given position.
Parameters: iterator pointing to the position after which the value needs to be inserted, values that need to be inserted Return value: iterator, pointing to the last inserted element
erase_after(iterator): It deletes the values after the given position.
Parameters: iterator, pointing to the position after which the value needs to be deleted
Return value: void
remove(element): It removes all the occurrences of the given element.
Parameters: element that needs to be deleted
Return value: void
front(): It returns the first element of the list.
Parameters: None
Return value: void
begin(): It returns an iterator pointing to the first element of the list.
Parameters: None
Return value: iterator, pointing to the first element
end(): It returns an iterator pointing to the last element of the list.
Parameters: None
Return value: iterator, pointing to the last element
max_size(): It returns the maximum number of elements that can be stored by the forward_list.
Parameters: None
Return value: integer, the maximum size
empty(): It tells us whether the list is empty or not
Parameters: None
Return value: boolean, true if the list is empty otherwise false.
reverse(): It reverses the list.
Parameters: None
Return value: void
#include<iostream>
#include<forward_list>
using namespace std;
void print(forward_list <int> l)
{
forward_list<int>::iterator itr;
for(itr=l.begin();itr!=l.end();++itr)
cout<<*itr<<' ';
cout<<'\n';
}
int main()
{
forward_list<int> l={1,2,3};
for(int i=0;i<5;++i)
{
l.push_front(i);
}
print(l);
cout<<"First element " <<l.front()<<'\n';
l.pop_front();
l.insert_after(l.begin(),{5,6,7});
print(l);
l.erase_after(l.begin());
print(l);
l.remove(2);
cout<<"List after removing all occurrences of 2: ";
print(l);
if(!l.empty()){
cout<<"Max size of list "<<l.max_size()<<'\n';
}
return 0;
}Output
4 3 2 1 0 1 2 3
First element 4
3 5 6 7 2 1 0 1 2 3
3 6 7 2 1 0 1 2 3
List after removing all occurrences of 2: 3 6 7 1 0 1 3
Max size of list 576460752303423487