The C++ Standard Library provides a family of type-safe, generic containers for storing collections of related objects. Each container is a class template parameterized on the element type, so aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/MicrosoftDocs/cpp-docs/llms.txt
Use this file to discover all available pages before exploring further.
std::vector<int> and a std::vector<std::string> are distinct types sharing the same implementation. Containers manage their own memory, support range-based for loops through iterators, and integrate seamlessly with the standard algorithms in <algorithm> and <numeric>.
When in doubt about which container to use, start with
std::vector. It stores elements contiguously in memory, offers O(1) random access and amortized O(1) push-back, and has the best cache locality of any sequence container.Container Categories
The Standard Library divides containers into three main families:Sequence Containers
Maintain the insertion order you specify:
vector, deque, list, forward_list, array.Associative Containers
Keep elements in sorted order by key:
map, set, multimap, multiset.Unordered Containers
Hash-based O(1) average lookup:
unordered_map, unordered_set, unordered_multimap, unordered_multiset.Sequence Containers
Sequence containers store elements in a linear arrangement and preserve the order in which you insert them.std::vector — Dynamic Array
vector is the go-to sequence container. It allocates contiguous memory, supports O(1) random access via index, and grows automatically as elements are added. Iterators, pointers, and references are invalidated after any reallocation.
std::array — Fixed-Size Array
array wraps a C-style array in a class template while preserving its fixed-size, stack-allocated nature. Unlike vector, the size is part of the type.
std::deque — Double-Ended Queue
deque offers O(1) insertions and removals at both ends, with O(1) random access. It does not store elements contiguously.
std::list — Doubly Linked List
list provides O(1) insertion and erasure anywhere given an iterator, and bidirectional traversal. It does not support random access.
std::forward_list — Singly Linked List
forward_list is the forward-iteration-only version of list. It has a smaller memory footprint and slightly faster insertions after a given position, but supports only forward iterators.
Associative Containers
Ordered associative containers store elements sorted by key usingoperator< by default. Lookup, insertion, and removal are all O(log n).
std::map — Key/Value Dictionary
map stores unique key/value pairs sorted by key. Access by key with [] or .at(). C++20 adds .contains().
std::set — Unique Sorted Elements
set stores unique elements in ascending order. It is effectively a map where the value and the key are the same element.
std::multimap and std::multiset
multimap allows multiple values per key; multiset allows duplicate elements. Both remain sorted.
Heterogeneous Lookup (C++14)
Ordered associative containers support heterogeneous lookup when you specifystd::less<> (the “diamond functor”) as the comparator. This avoids constructing a full key object for lookup:
Unordered (Hash) Containers
Unordered containers use a hash table internally. Average-case lookup, insertion, and removal are O(1) — better than ordered containers for large datasets — but worst-case is O(n) and iteration order is unspecified.std::unordered_map
std::unordered_set
Container Adapters
Container adapters wrap a sequence container and restrict its interface to model a specific abstract data type. They do not support iterators and cannot be used directly with standard algorithms.- stack (LIFO)
- queue (FIFO)
- priority_queue (max-heap)
Common Member Functions
Most containers share a consistent interface for common operations:| Operation | Member | Notes |
|---|---|---|
| Number of elements | size() | O(1) for all containers |
| Check if empty | empty() | Prefer over size() == 0 |
| Remove all elements | clear() | Does not release capacity in vector |
| Add at end | push_back(val) | Sequence containers |
| Construct at end | emplace_back(args…) | Avoids a copy/move |
| Add at front | push_front(val) | deque, list, forward_list |
| Insert at position | insert(it, val) | Returns iterator to inserted element |
| Remove at position | erase(it) | Returns iterator to next element |
| Find by key | find(key) | Associative/unordered containers |
| Swap two containers | swap(other) | O(1) for all containers |
| Access by key | at(key) / operator[] | map, unordered_map |
Iterators
Iterators are the bridge between containers and algorithms. Every container defines its own iterator types, but they all share a common interface.Choosing the Right Container
Need fast random access?
Need fast random access?
Use
std::vector or std::array. Both offer O(1) index access. Prefer array when the size is fixed at compile time.Need fast insertion/deletion anywhere?
Need fast insertion/deletion anywhere?
Use
std::list (doubly linked, O(1) anywhere given an iterator) or std::forward_list (singly linked, lower overhead, forward-only).Need fast key-based lookup?
Need fast key-based lookup?
Use
std::unordered_map or std::unordered_set for O(1) average-case hash lookup. Use std::map or std::set when sorted order or range queries (lower_bound, upper_bound) matter.Need fast insertion/deletion at both ends?
Need fast insertion/deletion at both ends?
Use
std::deque. It is faster than vector for front insertions and avoids element shifting.Need LIFO, FIFO, or priority access?
Need LIFO, FIFO, or priority access?
Use the adapters
std::stack, std::queue, or std::priority_queue respectively.