std::list is a sequence container implemented as a Doubly Linked List, while std::forward_list (C++11) is a lightweight Singly Linked List with zero memory overhead per node.
#include <iostream>
#include <list>
int main() {
std::list<int> values = {10, 25, 30, 25, 40};
// Remove all occurrences of 25 in O(N) time with zero reallocation
values.remove(25);
for (int v : values) {
std::cout << v << " ";
}
// Output: 10 30 40
std::cout << "\n";
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Compare cache locality of contiguous memory vs node-based lists.
Double-ended queue with chunked contiguous memory.
Bidirectional vs Forward iterators.