std::forward_list
std::forward_list
is a singly linked list. It’s similar to std::list
, but each element points only to the next element and not the previous. This reduces memory overhead compared to std::list
but at the cost of bidirectional iteration. Choose std::forward_list
when you require a list structure but don’t need to traverse backward and wish to save on memory overhead.
Purpose and suitability
The std::forward_list
is a singly linked list container in the STL. Its primary appeal lies in the following:
- Efficient insertions and deletions at any location in the list
- Consuming less memory than
std::list
since it doesn’t store previous pointers
It’s especially fitting in the following contexts:
- You require constant-time insertions or deletions irrespective of the position.
- Memory overhead is a concern.
- Bidirectional iteration is not needed.
While std::vector
excels in random access, turn to std::forward_list...