List and forward_list
The std::list is a doubly linked list, meaning that each element has one link to the next element and one link to its previous element. This makes it possible to iterate over the list both backwards and forwards. There is also a singly linked list named std::forward_list . The reason why you wouldn't always choose the doubly linked list over std::forward_list is because of the excessive memory occupied by the back pointers in the double linked list. So, if you don't need to traverse the list backwards, use std::forward_list. Another interesting feature of the forward list is that it's optimized for very short lists. When the list is empty, it only occupies one word, which makes it a good data structure for sparse data. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access