list & deque in C++
std::list is a doubly-linked list: O(1) insertion and erasure anywhere given an iterator, but O(n) indexed access (no operator[] at all) and pointer-chasing cache behavior. std::deque (double-ended queue) is the middle ground — O(1) push/pop at both ends with chunked storage that's friendlier to caches than list.
Practical rule: vector wins by default; deque wins for queues needing both-end growth; list wins only when iterator stability across inserts plus mid-container splicing genuinely matters — rarer than textbooks suggest.
list: nodes and iterators
Elements live in scattered nodes; insert/erase at a known iterator is O(1) and never invalidates other iterators.
No indexing on list
Reaching element k walks k links (O(n)) — if you index often, you picked the wrong container.
deque: both ends fast
push_front and push_back are both O(1), and operator[] exists — the natural double-ended buffer.
Choose by access pattern
Random access → vector; both-end growth → deque; stable iterators plus splicing → list.
#include <iostream>#include <deque>#include <list> int main() { std::deque<int> d; d.push_back(2); d.push_front(1); std::list<int> l{1, 2, 3}; l.push_front(0); std::cout << d[0] << d[1] << ", " << l.size() << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.