Containers library
Curated hub for choosing the right C++ container family: sequence, associative, unordered, adaptor, fixed-capacity, and view-like access tools.
The containers area is where most data-structure choices start in the C++ standard library. This hub helps you choose the right container family first, then the right concrete type, instead of starting from a flat list of names.
# Start Here
Sequence containers
General-purpose owning sequences when element order matters and you care about growth, insertion position, or iterator stability.
vector · deque · list · forward_list
Ordered associative containers
Key-based lookup with deterministic sorted order, tree-style traversal, and ordered range queries.
Unordered associative containers
Hash-table lookup when average-case key access matters more than sorted order or ordered traversal.
unordered_map · unordered_set · unordered_multimap · unordered_multiset
Container adaptors
Restricted interfaces layered on top of an underlying container when you want stack, queue, or priority semantics directly.
stack · queue · priority_queue
Fixed-capacity and contiguous storage
Use compile-time or fixed-capacity storage when size constraints are known and allocation strategy matters.
Views and access surfaces
Non-owning or layout-oriented views when you need to pass or interpret data without taking ownership.
# Quick Choice Map
| If you need... | Start with | Why | Common alternatives |
|---|---|---|---|
| A default owning sequence | vector | Contiguous storage, fast random access, and the best general-purpose default for most value sequences. | deque, list |
| Cheap insertion/removal at both ends | deque | Efficient front/back growth without the pointer-heavy tradeoffs of linked lists. | vector, list |
| Frequent splicing or stable node ownership | list or forward_list | Node-based structure favors stable references and link-level operations over cache locality. | deque, vector |
| Sorted key lookup and ordered iteration | map or set | Tree-based ordering gives deterministic traversal and range queries such as lower/upper bound. | unordered_map, flat_map |
| Fast average-case key lookup | unordered_map or unordered_set | Hash-based access is usually the starting point when ordering is irrelevant. | map, flat_map |
| Sorted associative storage with flat contiguous backing | flat_map or flat_set | Offers sorted semantics with vector-like storage tradeoffs, useful when lookup and locality dominate mutation cost. | map, unordered_map |
| A stack, FIFO queue, or heap-style priority interface | stack, queue, or priority_queue | These adaptors intentionally expose a narrower API than their underlying storage container. | deque, vector |
| Non-owning access to contiguous data | span | A view over existing memory when you do not want ownership or reallocation behavior. | mdspan, ranges views |
| Multidimensional indexing without owning storage | mdspan | Models extents, layout, and accessor policy for array-like multidimensional data. | span, array |
# Container Families
| Family | Representative types | Use it for |
|---|---|---|
| Sequence containers | vector, deque, list, forward_list | Owning ordered collections where element position matters more than key lookup. |
| Ordered associative containers | map, set, multimap, multiset | Sorted key-based storage with logarithmic lookup and ordered traversal. |
| Unordered associative containers | unordered_map, unordered_set, unordered_multimap, unordered_multiset | Hash-based key lookup when iteration order is not part of the problem. |
| Container adaptors | stack, queue, priority_queue | Restricted queue/stack/priority interfaces layered over another container. |
| Fixed-capacity and contiguous tools | array, inplace_vector, vector | Storage with strong locality properties where capacity model is a major choice. |
| View-like access surfaces | span, mdspan | Data access without ownership, especially for APIs, slices, and multidimensional layouts. |
| Flat associative additions | flat_map, flat_set, flat_multimap, flat_multiset | Sorted associative semantics backed by flat storage, introduced for newer locality-oriented use cases. |
# Ownership And Access Model
| Model | Choose it when | Typical types |
|---|---|---|
| Owning dynamic container | You want the container to manage element lifetime and usually allow growth or mutation. | vector, map, unordered_map |
| Owning fixed-size storage | The size or capacity model is constrained up front and allocation behavior matters. | array, inplace_vector |
| Non-owning contiguous view | You are borrowing data from elsewhere and want to pass it safely without copying. | span |
| Non-owning multidimensional view | You need indexing and layout metadata over existing multidimensional storage. | mdspan |
| Interface adaptor over another container | You want queue, stack, or heap behavior and intentionally do not want the full underlying container API. | stack, queue, priority_queue |
# Recent Standard Additions
| Standard | Notable additions | Why it changes navigation |
|---|---|---|
| C++11 | array, forward_list, unordered associative containers | Introduced modern fixed-size and hash-based container choices into the main decision tree. |
| C++20 | span | Made non-owning contiguous views a first-class route instead of treating borrowed arrays as raw pointers only. |
| C++23 | flat_map, flat_set, mdspan | Added locality-oriented sorted associative containers and multidimensional access models. |
| C++26 | inplace_vector | Provides a standard fixed-capacity vector-like option between `array` and dynamically growing `vector`. |
# Related Navigation
Ranges
Use ranges when the main problem is traversal pipelines, lazy views, and adaptor composition rather than container ownership.
Strings
`basic_string` and `basic_string_view` often feel container-like, but text, encodings, literals, and conversion concerns live in the string hub.
Named requirements
Use the requirements pages when you need the formal semantic contracts shared by container families.
SequenceContainer · AssociativeContainer · UnorderedAssociativeContainer
See also