As a C++ lead, would you make std::vector the default container in your guidelines, and which exceptions would you write into that rule?
answer
- contiguous and simple by default
- known size, bounded size, stable address
- reallocation spikes and dangling
- measure before deviating
basics
~20 sYes: std::vector is contiguous, cache-friendly and simple, so it should be the default sequence. Write exceptions for fixed sizes (std::array, C++26 std::inplace_vector), stable element addresses, front insertion, keyed lookup and packed bools, each backed by a stated reason or a measurement.
solid answer
~40 sI would make `std::vector` the default because it is contiguous, works with `data()`, `std::span` and every algorithm, has the lowest per-element overhead, and its weaknesses are well understood. The rule needs written exceptions with reasons: a size fixed at compile time uses `std::array`; a hard capacity bound with no heap, common in engines and embedded code, uses C++26 `std::inplace_vector` where the toolchain ships it, or a team-owned fixed-capacity type until then; elements whose addresses must survive growth use `std::deque`, `std::list` or a vector of `std::unique_ptr`; frequent insertion at the front uses `std::deque`; keyed lookup uses an associative container. I would also write down the costs a default hides: reallocation spikes and dangling pointers on growth, mitigated by `reserve` budgets, and `std::vector<bool>`'s proxy semantics. Deviations should cite a measurement or a semantic need.
go deeper
Know that std::vector is the usual default sequence container and that std::array covers a fixed compile-time size.
Explain why contiguity makes the vector a good default and name the cases where another container is needed: stable addresses, front insertion, keyed lookup.
Anticipate what the default costs in production: reallocation spikes, dangling pointers after growth, and held capacity. Apply reserve budgets and buffer reuse.
Own the guideline: default plus written exceptions, evidence required for performance deviations, team-owned fixed-capacity types, and a review of the rule when the C++ baseline moves.
## Why std::vector earns the default A guideline that says "use `std::vector` unless you have a reason not to" is common in C++ codebases, and the reasons are concrete: - **Contiguous storage.** Elements sit next to each other, so iteration walks memory linearly, `data()` hands the buffer to C APIs and GPUs, and `std::span` views it without copying. - **Lowest per-element overhead.** No node pointers or per-element allocations; the only overhead is unused capacity. - **Universal interoperability.** Every standard algorithm, range adaptor and serialization library accepts it. - **Well-known cost model.** Amortized constant `push_back`, linear insertion in the middle, and invalidation on reallocation are documented and teachable. For small and medium sizes, a vector often beats node-based containers even at operations where those have better asymptotics, because shifting a few hundred contiguous elements is cheap. The reasons belong to cache-friendly layout, a neighbouring topic, but the guideline can rely on them. ## The exceptions worth writing down | Situation | Choice | Reason | |---|---|---| | Size known at compile time, small | `std::array<T, N>` | no heap, size in the type | | Hard upper bound, no heap allowed | C++26 `std::inplace_vector<T, N>`, or an in-house fixed-capacity type | storage inside the object; overflow is an error, not an allocation | | Element addresses must survive growth | `std::deque`, `std::list`, or `std::vector<std::unique_ptr<T>>` | elements never move | | Frequent insertion at the front | `std::deque` | constant-time `push_front` | | Lookup by key | ordered or unordered associative containers, or a sorted vector | logarithmic or hashed lookup | | Many boolean flags written concurrently | `std::vector<std::uint8_t>` | `std::vector<bool>` packs bits behind a proxy | The `inplace_vector` row needs a version note. It was added in C++26: a contiguous container whose capacity is fixed and whose elements are stored within the object itself. Its `push_back` throws `std::bad_alloc` when full, `try_push_back` returns an empty `optional` instead, and `unchecked_push_back` makes a full container a precondition violation. GCC 16's libstdc++ ships the `<inplace_vector>` header, while the libc++ used for the other examples here does not yet, so this snippet follows the draft and was not compiled: ```cpp // C++26 - not compiled here: this libc++ lacks <inplace_vector> #include <inplace_vector> std::inplace_vector<Emitter, 8> emitters; // storage inside the object if (!emitters.try_push_back(Emitter{})) // empty optional when full log_emitter_budget_exceeded(); ``` ## The costs a default hides A default is only safe when people know what it costs, so the guideline should name: 1. **Reallocation latency.** Crossing a capacity boundary transfers every element at once. In a game loop that is a frame-time spike; the mitigation is a `reserve` budget per subsystem and reusing buffers across frames. 2. **Invalidation on growth.** Pointers, references and iterators into a vector die when it reallocates. Code that hands out element pointers needs stable storage or indices instead. 3. **Held capacity.** A vector keeps its high-water mark; long-lived buffers that spike once may need `shrink_to_fit` or a swap. 4. **The `std::vector<bool>` exception.** It is a bit-packed specialization with proxy references, and it breaks generic code. ## Enforcing it without dogma - Write the rule as a default with listed exceptions, not a ban, and require that each deviation states its reason in a comment or review. - Ask for a measurement when the reason is performance; the expected winner frequently loses on real data sizes. - Provide team-owned alternatives where the standard is not yet available, such as a fixed-capacity vector until C++26 reaches your toolchains, and plan its replacement by `std::inplace_vector`. - Revisit the exceptions when the baseline moves: a C++26 upgrade changes the fixed-capacity answer. - Make exceptions visible: a review checklist item for new `std::list` and `std::vector<bool>` uses, and a short page listing the approved alternatives with one example each, keeps the rule from turning into folklore. ## How an interviewer reads the answer A lead is expected to say yes, explain why, and show judgment about the exceptions and the costs. An answer that only says "vector is fastest" misses the semantic exceptions, such as stable addresses; an answer that lists containers by asymptotic complexity alone misses why the default exists.
- What does std::inplace_vector do when push_back is called on a full container, and what are the alternatives?In the C++26 draft, any member function that would push the size past the capacity throws `std::bad_alloc`, so `push_back` on a full `inplace_vector` throws. `try_push_back` and `try_emplace_back` return an empty `optional` and leave the container unchanged, and `unchecked_push_back` makes "not full" a precondition, for hot paths that have already checked the size.
- When would you approve a std::list or std::deque instead of the default std::vector in review?When the code needs a guarantee a vector cannot give, not just a better asymptotic cost: element addresses that must stay valid while the container grows, constant-time insertion at the front, or splicing nodes between containers. For a pure speed argument, I would ask for a benchmark on realistic sizes, because the vector often wins anyway.
- std::list is the right default because insertion is O(1)
- std::vector should never be used for more than a few thousand elements
- std::inplace_vector is available in every C++23 toolchain
- A default container rule means deviations are never allowed
- Element pointers into a std::vector stay valid as it grows
Assumes C++23 as the published baseline; std::inplace_vector is C++26, shipped in GCC 16's libstdc++ but not yet in the libc++ used for these examples.