Forward list in C++
In STL (Standard Template Library), a sequential container is called forward_list. It is quite similar to the STL list, except forward_list keeps the location of the following element, saving memory.
In contrast, the list also stores the location of the preceding and subsequent list elements. In forward_list, insertion and deletion are swift and take only constant time. In memory, it is implemented as a single LinkedList. The header file with the #include forward_list> tag contains forward_lists.
Syntax:
forward_list<datatype> name;
Parameter
datatype- You must give what type of data you want to store.
Properties of containers
- Sequence containers have elements that are strictly arranged in a linear order. Access to specific elements is determined by where they appear in the list.
- The linked list element stores information on where to find the subsequent element, enabling constant time to insert and erase operations following a particular element (even of vast ranges) but preventing direct random access.
- The allocator-aware container uses an allocator object to manage its storage requirements dynamically.
Member Types in C++
- allocator_type – A type that symbolizes the forward list object's allocator class.
- const_iterator - The type that offers the forward list's iterator as a constant.
- const_pointer - An element type offers a pointer to a const element in a forward list.
- const_reference - the type that offers an unchanging reference to an entry in the forward list.
- difference_type – A signed integer indicating the number of forward list elements in the interval between iterator-pointed elements.
- iterator – A type that offers an iterator for the forward list.
- pointer - A type that references an element in the forward list.
- reference - A type that offers a pointer to an entry in the forward list.
- size_type - A type that shows the unsigned separation of two items.
- value_type - A type that specifies the kind of element kept in a forward list.
Member Functions in C++
- assign - The assignment function removes elements from a forward list and adds a fresh set to the target forward list.
- before_begin - A forward list's position before the first member is addressed by an iterator that is returned.
- begin - Gives back an iterator that starts at the first item in a forward list.
- cbefore_begin - A const iterator that addresses the location before the first entry in a forward list is returned.
- cbegin - Returns a const iterator addressing the first element in a forward list.
- cend - Returns a const iterator directed at the point in a forward list following the last element.
- clear - Removes every component from a forward list.
- emplace_after - Move creates a new element at a given position.
- emplace_front - Adds a newly built element to the list's beginning.
- empty - Examines the existence of an empty forward list.
- end - Returns a forward list iterator that starts at the position after the last entry.
- erase_after - The forward list's elements are removed after a certain position.
- front - A reference to the first item in a forward list is returned.
- get_allocator - Returns a duplicate of the forward list's allocator object.
- insert_after - The forward list is expanded by adding items after a defined location.
- max_size - Returns the longest possible forward list.
- merge - The components are taken out of the argument list, added to the target forward list, and then the new, combined set of elements is arranged in ascending order or according to some other predetermined order.
- pop_front - Removes the first element from a forward list.
- push_front - Adds a new element to a forward list's first position.
- remove - Deletes items from a forward list that match a given value.
- remove_if - Eliminates items from a forward list for which a given predicate is true.
- resize - Gives updated instructions for a forward list's size.
- reverse - A forward list that reverses the order of the elements.
- sort - Places the components in ascending order or according to a predicate-determined order.
- splice_after - Link repairs between nodes.
- swap - Two forward lists' elements are switched.
- unique - Removes neighboring components that satisfy a certain condition.
Example for Forward list
Look at the following simple example of a forward list:
Code:
#include <iostream>
#include <forward_list>
int main() {
// Create a forward list of strings
std::forward_list<std::string> myList;
// Insert elements at the beginning
myList.push_front("chocolates");
myList.push_front("biscuits");
myList.push_front("chips");
// Iterate through the list and print its elements
for (const std::string& snacks : myList) {
std::cout << snacks << " ";
}
std::cout << std::endl;
// Insert an element after the first element
auto it = myList.begin();
++it; // Move to the second element
myList.insert_after(it, "puffs");
// Erase an element after the second element
myList.erase_after(it);
// Print the modified list
for (const std::string& snacks : myList) {
std::cout << snacks << " ";
}
std::cout << std::endl;
return 0;
}
Output:
![Forward list in C++](https://static.tutorialandexample.com/cpp/forward-list-in-cpp1.png)
We are using the C++ Standard Library's std::forward_list container in this example. We build a forward list of strings, add elements using push_front, iterate through the list with a range-based for loop, and then change the list with insert_after and erase_after functions..