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, thus increasing the storage space required to store each element. The drawback of forward list is that it cannot be iterated backwards and its individual elements cannot be accessed directly.
Forward List is preferred over list when only forward traversal is required (same as singly linked list is preferred over doubly linked list) as we can save space. Some example cases are, chaining in hashing, adjacency list representation of graph, etc.
Operations on Forward List :
1. assign() :- This function is used to assign values to forward list, its another variant is used to assign repeated elements.
The elements of first forward list are : 1 2 3 The elements of second forward list are : 10 10 10 10 10
2. push_front() :- This function is used to insert the element at the first position on forward list. The value from this function is copied to the space before first element in the container. The size of forward list increases by 1.
3. emplace_front() :- This function is similar to the previous function but in this no copying operation occurs, the element is created directly at the memory before the first element of the forward list.
4. pop_front() :- This function is used to delete the first element of list.
The forward list after push_front operation : 60 10 20 30 40 50 The forward list after emplace_front operation : 70 60 10 20 30 40 50 The forward list after pop_front operation : 60 10 20 30 40 50
4. insert_after() This function gives us a choice to insert elements at any position in forward list. The arguments in this function are copied at the desired position.
5. emplace_after() This function also does the same operation as above function but the elements are directly made without any copy operation.
6. erase_after() This function is used to erase elements from a particular position in the forward list.
The forward list after insert_after operation : 10 1 2 3 20 30 The forward list after emplace_after operation : 10 1 2 3 2 20 30 The forward list after erase_after operation : 10 1 2 3 2 30
7. remove() :- This function removes the particular element from the forward list mentioned in its argument.
8. remove_if() :- This function removes according to the condition in its argument.
The forward list after remove operation : 10 20 30 25 The forward list after remove_if operation : 10 20
9. splice_after() :- This function transfers elements from one forward list to other.
The forward list after splice_after operation : 40 10 20 30 50 60
Some more methods of forward_list:
- front()– This function is used to reference the first element of the forward list container.
- begin()– begin() function is used to return an iterator pointing to the first element of the forward list container.
- end()– end() function is used to return an iterator pointing to the last element of the list container.
- cbegin()– Returns a constant iterator pointing to the first element of the forward_list.
- cend()– Returns a constant iterator pointing to the past-the-last element of the forward_list.
- before_begin()– Returns a iterator which points to the position before the first element of the forward_list.
- cbefore_begin()– Returns a constant random access iterator which points to the position before the first element of the forward_list.
- max_size()– Returns the maximum number of elements can be held by forward_list.
- resize()– Changes the size of forward_list.
This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Forward List in C++ | Set 2 (Manipulating Functions)
- Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
- List in C++ | Set 2 (Some Useful Functions)
- What are Forward declarations in C++
- Forward Iterators in C++
- Implementing Forward Iterator in BST
- Functions in C/C++
- C | Functions | Question 11
- C | Functions | Question 10
- C | Functions | Question 9
- C | Functions | Question 8
- C | Functions | Question 7
- C | Functions | Question 6
- Searching in a map using std::map functions in C++
- Macros vs Functions