C++ STL Forward List Container – std::forward_list

In this tutorial you will learn about C++ STL forward list i.e., std::forward_list and operations, methods applicable on it.

Forward Lists come under sequence containers. Forward List implements singly linked list. Insertion, removal and moving operations are very fast than other containers. Every element of forward list contains its next element address. Main disadvantage of forward list is that cannot be iterated backwards and its individual elements cannot be accessed directly. When singly linked list preferred over double linked list, Forward list is better than List. Because list behaves a like double linked list. Example for where we can use forward list is chaining in hashing, adjacency list representation of graph etc.

Also Read: C++ STL List Container – std::list

C++ STL Forward List

Now let’s see what are the operations we can apply on forward list:

assign(): Just like insert method, assign() will store the values. By using assign() we can insert elements in two ways.

First way is we can insert what are the elements we required. Second way is we can insert a single element “n” number of times. But in second way of assignment list will be replaced by new values. Old list values will be erased.

Try this below code for more understanding:


10 20 30
99 99 99

Insert Functions

push_front(): This function used to insert new value at beginning of the list. The value from this function copied to the address before to the current first element of container.

emplace_front(): This function also used to insert elements at beginning of the container but there no copy operation. Here element directly inserts at the address before to the first element.

insert_after(): Using this function we can insert element at any position of the forward list. The arguments which we pass to this function will be copied to particular location.

emplace_after(): This also works same as insert_after() function. But element directly insert without any copy operation.

Example program to show above insert functions:


4 5 10 20 30
4 99 0 5 10 20 30

Delete Functions

pop_front(): This function removes the first element of the forward list.

erase_after(): This function used to delete the element at particular position of forward list.

remove(): This function removes the specific element which we pass as an argument to this function

remove_if(): This function removes the element we specified only the condition which we gives is true.

Example program to show above erasing functions of forward list:


20 40 50

Some more functions are:

splice_after(): This function used to transfer one forward list elements to other forward list. It places the elements after specified position only.

reverse(): This function reverse all elements of the forward list.

sort(): This function sorts the elements in the forward list.

Example program to explain above functions:


after adding forward list1 into forward list2
30 20 40 60 50 70
after reversing forward list2
70 50 60 40 20 30
after sorting the elements
20 30 40 50 60 70

Comment below if you have queries or found any information incorrect in above tutorial for C++ STL forward list.

1 thought on “C++ STL Forward List Container – std::forward_list”

Leave a Comment

Your email address will not be published. Required fields are marked *