C++ Library - <forward_list>
Introduction
forward_list is a popularly used sequence container. Container is an object that holds data of same type. forward_list container is implemented as singly linked-list, hence it provides unidirectional sequential access to it's data.
forward_list doesn't provide fast random access, it only supports sequential access in only one directions. forward_list allows insertion and deletion operation anywhere within a sequence in constant time.
Elements of forward_list can be scattered in different chunks of memory. Container stores necessary information to allow sequential access to it's data. forward_lists can shrink or expand as needed from both ends at run time. The storage requirement is fulfilled automatically by internal allocator.
Zero sized forward_lists are also valid. In that case forward_list.begin() and forward_list.end() points to same location. But behavior of calling front() is undefined.
Definition
Below is definition of std::forward_list from <forward_list> header file
template < class T, class Alloc = allocator<T> > class forward_list;
Parameters
T − Type of the element contained.
T may be substituted by any other data type including user-defined type.
Alloc − Type of allocator object.
By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent.
Member types
Following member types can be used as parameters or return type by member functions.
| Sr.No. | Member types | Definition |
|---|---|---|
| 1 | value_type | T (First parameter of the template) |
| 2 | allocator_type | Alloc (Second parameter of the template) |
| 3 | reference | value_type& |
| 4 | const_reference | const value_type& |
| 5 | pointer | value_type* |
| 6 | const_pointer | const value_type* |
| 7 | iterator | a random access iterator to value_type |
| 8 | const_iterator | a random access iterator to const value_type |
| 9 | size_type | size_t |
| 10 | difference_type | ptrdiff_t |
Functions from <forward_list>
Below is list of all methods from <forward_list> header.
Constructors
Destructor
Member functions
Non-member overloaded functions
| Sr.No. | Method & Description |
|---|---|
| 1 | operator==
Tests whether two forward_lists are equal or not. |
| 2 | operator!=
Tests whether two forward_lists are equal or not. |
| 3 | operator<
Tests whether first forward_list is less than other or not. |
| 4 | operator<=
Tests whether first forward_list is less than or equal to other or not. |
| 5 | operator>
Tests whether first forward_list is greater than other or not. |
| 6 | operator>=
Tests whether first forward_list is greater than or equal to other or not. |
| 7 | swap
Exchanges the contents of two forward_list. |