⛓️ std::list (Doubly Linked List) 🚀
What it is: A sequence container that implements a doubly linked list. Each element is stored in a separate node, and each node contains the data plus pointers to the next and previous nodes in the sequence.
Core Benefit: Provides extremely fast insertion and deletion of elements anywhere in the list (front, back, or middle) once the position is known, without invalidating iterators to other elements.
✨ Essentials & Performance
| Operation | Code Example | Time Complexity | Notes |
|---|---|---|---|
| Include | #include <list> |
||
| Declaration | std::list<int> L; |
Empty list. | |
| Insert/Delete (Front) | L.push_front(10);L.pop_front(); |
O(1) | Extremely fast manipulation at the beginning. |
| Insert/Delete (Back) | L.push_back(20);L.pop_back(); |
O(1) | Extremely fast manipulation at the end. |
| Insert/Delete (Middle) | L.insert(it, 30);L.erase(it); |
O(1) | Requires an O(N) traversal to find the iterator it. |
| Random Access | L.at(2) or L[2] |
O(N) | Not supported; requires slow traversal from the beginning. |
| Merge Lists | L1.merge(L2); |
O(N) | Merges L2 into L1. L2 becomes empty. |
| Splice | L1.splice(it, L2); |
O(1) | Moves all elements from L2 to L1 at position it. The list nodes are moved, not copied. |
| Sort | L.sort(); |
$O(N \log N)$ | Fast internal sort that avoids memory reallocation. |
| Clear | L.clear(); |
O(N) | Removes all nodes. |
💡 Key Characteristics
- Non-Contiguous Memory: Elements are scattered throughout memory. This results in poor cache performance compared to
std::vectororstd::deque. - Iterator Stability: Iterators (pointers to nodes) remain valid even when elements are inserted or deleted around them. They are only invalidated if the node they point to is explicitly deleted.
- No
capacity()orreserve(): Lists allocate memory per node, so the concepts of capacity and reservation don't apply. - Memory Overhead: Higher memory consumption than
std::vectorbecause every element node requires extra space for two pointers (next and previous).
⚠️ When to Use std::list
Use std::list only when your primary need is:
- Frequent insertion and deletion in the middle of the container.
- Moving elements/sub-ranges efficiently using
splice()(O(1)). - Maintaining iterator stability despite extensive list modification.
Avoid std::list if you need random access (accessing by index) or maximum memory efficiency.