Roughing it with std::forward_list<T>

The standard container std::forward_list<T> is a linked list like std::list, but with fewer amenities--no way to get its size, no way to iterate backward. In memory it looks similar to std::list<T>, but with smaller nodes:

Nevertheless, std::forward_list retains almost all of the "special skills" of std::list. The only operations that it can't do are splice (because that involves inserting "before" the given iterator) and push_back (because that involves finding the end of the list in constant time).

forward_list replaces these missing member functions with _after versions:

  • flst.erase_after(it) to erase ...

Get Mastering the C++17 STL now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.