Containers library
A guide to Chapter 23: container requirements, sequence containers, associative containers, unordered containers, container adaptors and views; a container's contract is read along four lines: complexity, iterator invalidation, exception safety and allocators.
Key points
- The clauses for the individual containers are short; most of the semantics live in the groups of general requirements in [container.requirements]. To read a container, take the general requirements and that container's own additional clauses together.
- A container's contract falls into four categories: complexity, when iterators and references are invalidated, exception-safety guarantees, and how the allocator is used.
- Container function preconditions come in several strengths: an unmet Constraints makes the overload non-viable, an unmet Mandates is a compile error, a violated Preconditions is UB, and a violated C++26 hardened precondition terminates the program in a hardened implementation.
basic_string,bitsetandvalarrayare not in this chapter, butbasic_stringmeets this chapter's container requirements.
What the chapter contains ([containers.general])#
¶1 This chapter describes components that programs use to organize collections of information. Table 74 in ¶2 lists its six parts:
| Subclause | Contents | Headers | Notes on this site |
|---|---|---|---|
| [container.requirements] | Requirements for general, allocator-aware, sequence, associative and unordered containers; node handles | none | [container.requirements], [container.requirements.general], [sequence.reqmts], [container.node], [associative.reqmts], [unord.req] |
| [sequences] | Sequence containers | <array> <deque> <forward_list> <hive> <inplace_vector> <list> <vector> |
[sequences], [array], [deque], [forward.list], [hive], [list], [vector], [vector.bool], [inplace.vector] |
| [associative] | Ordered associative containers | <map> <set> |
[associative] |
| [unord] | Unordered associative containers | <unordered_map> <unordered_set> |
[unord] |
| [container.adaptors] | Container adaptors | <queue> <stack> <flat_map> <flat_set> |
[container.adaptors], [flat.map], [flat.set] |
| [views] | Views | <span> <mdspan> |
[views], [views.contiguous], [views.multidim] |
Several types often thought of as containers are not in this chapter: basic_string is in Chapter 27 ([basic.string]) but meets this chapter's contiguous-container and allocator-aware-container requirements; bitset is in Chapter 22; valarray is in Chapter 29; basic_stacktrace is in Chapter 19 and meets the allocator-aware container requirements. Iterator concepts and range adaptors are in Chapters 24 and 25 ([iterators], [ranges]).
Four aspects of a container contract#
Nearly every rule for a specific container belongs to one of the four categories below, and the container pages on this site are organized along them:
- Complexity. The standard counts only operations on elements ([container.requirements]), but these requirements effectively dictate the data structure:
vectormust grow geometrically,dequemust be built from blocks,unordered_mapmust have one node per element. - Invalidation. Which operations make which iterators, pointers and references stop being valid. The general principle is that nothing is invalidated unless it is specified to be ([container.reqmts]/67).
- Exception safety. By default, a single-element insertion that throws has no effect and erasure does not throw ([container.reqmts]/66);
vector,deque, the associative containers and the unordered containers each have exceptions. - Allocators. Elements may be constructed and destroyed only through
allocator_traits::constructanddestroy; how the allocator is propagated on copy, move, assignment and swap is decided by the three propagate traits ([container.requirements.general]). The allocator protocol itself is in [mem] and [allocator.traits].
[container.requirements] collects each container's rules on the last three aspects into comparison tables.
Strengths of preconditions#
The function descriptions in this chapter use the elements defined in [structure.specifications], and violating each has a different consequence:
| Element | Example | Consequence of violation |
|---|---|---|
| Constraints | insert(InputIterator, InputIterator) requires the arguments to qualify as input iterators; container-compatible-range |
Does not participate in overload resolution |
| Mandates | vector<T, A> requires A::value_type to be T |
Ill-formed, diagnostic required |
| Preconditions | in insert(p, i, j), i and j are not iterators into the container itself; the element type is Cpp17CopyInsertable |
UB; an unmet type requirement usually shows up as an instantiation error |
| Hardened preconditions C++26 | front(), back(), pop_* and operator[] require non-empty and in-bounds |
Terminates the program in a hardened implementation, UB otherwise |
History and evolution#
| Version | Main changes to this chapter |
|---|---|
| C++98 | The STL enters the standard: vector, deque, list, set, map and their multi versions, stack, queue, priority_queue, vector<bool> |
| C++11 | array, forward_list, unordered_*; move semantics and emplace; allocator_traits; size() must be constant time; initializer_list constructors |
| C++14 | Heterogeneous lookup in associative containers (transparent comparators) |
| C++17 | Node handles, extract, merge; try_emplace, insert_or_assign; std::pmr aliases; vector, list and forward_list allow incomplete element types |
| C++20 | std::erase, erase_if; contains; constexpr vector; <=>; span; heterogeneous lookup in unordered containers |
| C++23 | flat_map, flat_set; mdspan; from_range constructors and *_range member functions; iterator-pair constructors for stack and queue |
| C++26 | inplace_vector, hive; standard library hardening; deque, list, forward_list, the associative containers, the unordered containers and the container adaptors usable in constant evaluation; heterogeneous insertion into associative containers; span::at; submdspan |
libc++ in Clang 23 supports the C++26 parts only partially. constexpr for list, forward_list, map, set, flat_map, flat_set and queue is implemented (the corresponding __cpp_lib_constexpr_* macros are 202502L), but not for deque; <inplace_vector> and <hive> are not provided yet; heterogeneous insertion into associative containers is not implemented either.
#include <list>
#include <map>
#include <vector>
constexpr int sum() {
std::list<int> l{1, 2, 3};
std::map<int, int> m{{1, 10}, {2, 20}};
std::vector<int> v(l.begin(), l.end());
int s = 0;
for (int x : v) s += x;
return s + m.at(2);
}
static_assert(sum() == 26);
int main() {}Contents
- 23.1General[containers.general]
- 23.2Requirements[container.requirements]done
- 23.2.1Preamble[container.requirements.pre]
- 23.2.2General containers[container.requirements.general]done
- 23.2.3Container data races[container.requirements.dataraces]
- 23.2.4Sequence containers[sequence.reqmts]done
- 23.2.5Node handles[container.node]done
- 23.2.6Insert return type[container.insert.return]
- 23.2.7Associative containers[associative.reqmts]done
- 23.2.8Unordered associative containers[unord.req]done
- 23.3Sequence containers[sequences]done
- 23.3.1General[sequences.general]
- 23.3.2Header <array> synopsis[array.syn]
- 23.3.3Class template array[array]done
- 23.3.4Header <deque> synopsis[deque.syn]
- 23.3.5Class template deque[deque]done
- 23.3.6Header <forward_list> synopsis[forward.list.syn]
- 23.3.7Class template forward_list[forward.list]done
- 23.3.8Header <hive> synopsis[hive.syn]
- 23.3.9Class template hive[hive]done
- 23.3.10Header <list> synopsis[list.syn]
- 23.3.11Class template list[list]done
- 23.3.12Header <vector> synopsis[vector.syn]
- 23.3.13Class template vector[vector]done
- 23.3.14Specialization of vector for bool[vector.bool]done
- 23.3.15Header <inplace_vector> synopsis[inplace.vector.syn]
- 23.3.16Class template inplace_vector[inplace.vector]draft
- 23.4Associative containers[associative]done
- 23.5Unordered associative containers[unord]done
- 23.5.1General[unord.general]
- 23.5.2Header <unordered_map> synopsis[unord.map.syn]
- 23.5.3Class template unordered_map[unord.map]
- 23.5.4Class template unordered_multimap[unord.multimap]
- 23.5.5Header <unordered_set> synopsis[unord.set.syn]
- 23.5.6Class template unordered_set[unord.set]
- 23.5.7Class template unordered_multiset[unord.multiset]
- 23.6Container adaptors[container.adaptors]done
- 23.6.1General[container.adaptors.general]
- 23.6.2Header <queue> synopsis[queue.syn]
- 23.6.3Class template queue[queue]
- 23.6.4Class template priority_queue[priority.queue]
- 23.6.5Header <stack> synopsis[stack.syn]
- 23.6.6Class template stack[stack]
- 23.6.7Header <flat_map> synopsis[flat.map.syn]
- 23.6.8Class template flat_map[flat.map]done
- 23.6.9Class template flat_multimap[flat.multimap]
- 23.6.10Header <flat_set> synopsis[flat.set.syn]
- 23.6.11Class template flat_set[flat.set]done
- 23.6.12Class template flat_multiset[flat.multiset]
- 23.6.13Container adaptors formatting[container.adaptors.format]
- 23.7Views[views]done
- 23.7.1General[views.general]
- 23.7.2Contiguous access[views.contiguous]done
- 23.7.3Multidimensional access[views.multidim]done
- 23.7.3.1Overview[mdspan.overview]
- 23.7.3.2Header <mdspan> synopsis[mdspan.syn]
- 23.7.3.3Class template extents[mdspan.extents]
- 23.7.3.3.1Overview[mdspan.extents.overview]
- 23.7.3.3.2Exposition-only helpers[mdspan.extents.expo]
- 23.7.3.3.3Constructors[mdspan.extents.cons]
- 23.7.3.3.4Observers of the multidimensional index space[mdspan.extents.obs]
- 23.7.3.3.5Comparison operators[mdspan.extents.cmp]
- 23.7.3.3.6Alias template dextents[mdspan.extents.dextents]
- 23.7.3.3.7Alias template dims[mdspan.extents.dims]
- 23.7.3.4Layout mapping[mdspan.layout]
- 23.7.3.4.1General[mdspan.layout.general]
- 23.7.3.4.2Requirements[mdspan.layout.reqmts]
- 23.7.3.4.3Layout mapping policy requirements[mdspan.layout.policy.reqmts]
- 23.7.3.4.4Layout mapping policies[mdspan.layout.policy.overview]
- 23.7.3.4.5Class template layout_left::mapping[mdspan.layout.left]
- 23.7.3.4.6Class template layout_right::mapping[mdspan.layout.right]
- 23.7.3.4.7Class template layout_stride::mapping[mdspan.layout.stride]
- 23.7.3.4.8Class template layout_left_padded::mapping[mdspan.layout.leftpad]
- 23.7.3.4.9Class template layout_right_padded::mapping[mdspan.layout.rightpad]
- 23.7.3.5Accessor policy[mdspan.accessor]
- 23.7.3.6Class template mdspan[mdspan.mdspan]
- 23.7.3.7submdspan[mdspan.sub]
- 23.7.3.7.1Overview[mdspan.sub.overview]
- 23.7.3.7.2Range slices[mdspan.sub.range.slices]
- 23.7.3.7.3submdspan_mapping_result[mdspan.sub.map.result]
- 23.7.3.7.4Exposition-only helpers[mdspan.sub.helpers]
- 23.7.3.7.5submdspan slice canonicalization[mdspan.sub.canonical]
- 23.7.3.7.6subextents function[mdspan.sub.extents]
- 23.7.3.7.7Specializations of submdspan_mapping[mdspan.sub.map]
- 23.7.3.7.7.1Sliceable layout mapping requirements[mdspan.sub.map.sliceable]
- 23.7.3.7.7.2Common[mdspan.sub.map.common]
- 23.7.3.7.7.3layout_left specialization of submdspan_mapping[mdspan.sub.map.left]
- 23.7.3.7.7.4layout_right specialization of submdspan_mapping[mdspan.sub.map.right]
- 23.7.3.7.7.5layout_stride specialization of submdspan_mapping[mdspan.sub.map.stride]
- 23.7.3.7.7.6layout_left_padded specialization of submdspan_mapping[mdspan.sub.map.leftpad]
- 23.7.3.7.7.7layout_right_padded specialization of submdspan_mapping[mdspan.sub.map.rightpad]
- 23.7.3.7.8submdspan function template[mdspan.sub.sub]
- 23.7.3.8Multidimensional copy algorithms[mdspan.copy]