What are the performance tradeoffs between std::vector and std::list? In what scenario is a std::list more efficient?
Last updated
std::vectorstores elements contiguously: cache-friendly, O(1) random access, low overhead, but middle insert/erase is O(n) and growth can invalidate iterators.std::list(doubly-linked) gives O(1) insert/erase at a known iterator, stable iterators, andsplice(), but has no random access and poor cache locality from per-node allocations.List wins only for many mid-sequence insertions/erasures where you already hold the iterator (especially with large/expensive-to-move elements) or heavy splicing. In practice vector often still wins due to cache effects.