A container is an object that stores a collection of elements, manages their memory, and provides methods for interacting with the collection. Standard library containers are class templates, so the element type is given as a template argument:
std::vector#include<vector><int> v;std::set#include<set><std::string#include<string>> s;std::map#include<map><std::string#include<string>, int> m;
Containers can be categorised depending on how the elements are stored:
Sequence container: element order is preserved in the order they are inserted.
Associative container: elements are sorted by a key in a balanced binary tree.
Container adapter: wraps another container to restrict the interface.
Name
Description
Category
Example
std::vector#include<vector>
Dynamically sized array
Sequence
std::vector#include<vector><int> v(3, 0);
std::deque
Double-ended queue
Sequence
std::deque<int> d(3, 0);
std::list
Doubly linked list
Sequence
std::list<int> l(3, 0);
std::string#include<string>
Dynamically sized array of characters
Sequence
std::string#include<string> s("abc");
std::set#include<set>
Sorted collection of unique element
Associative
std::set#include<set><int> s;
std::multiset#include<set>
Sorted collection that allows duplicate elements
Associative
std::multiset#include<set><int> s;
std::map#include<map>
Sorted key-value pairs with unique keys
Associative
std::map#include<map><std::string#include<string>, int> m;
std::multimap
Sorted key-value pairs that allows duplicate keys
Associative
std::multimap<std::string#include<string>, int> m;
std::stack
Last in, first out
Adapter
std::stack<int> s;
std::queue
First in, first out
Adapter
std::queue<int> q;
std::priority_queue
Largest (or prioritised) element first
Adapter
std::priority_queue<int> q;
Name
Description
Category
Example
std::array#include<array>
Fixed-size array
Sequence
std::array#include<array><int, 3> a = {1, 2, 3};
std::forward_list
Singly linked list
Sequence
std::forward_list<int> l = {1, 2, 3};
std::unordered_set#include<unordered_set>
Hashed collection of unique elements
Unordered associative
std::unordered_set#include<unordered_set><int> s = {1, 2, 3};
std::unordered_multiset#include<unordered_set>
Hashed collection that allows duplicate keys
Unordered associative
std::unordered_multiset#include<unordered_set><int> s = {1, 1, 2};
std::unordered_map
Hashed key-value pairs with unique keys
Unordered associative
std::unordered_map<std::string#include<string>, int> m = {{"a", 1}};
std::unordered_multimap
Hashed key-value pairs that allows duplicate keys
Unordered associative
std::unordered_multimap<std::string#include<string>, int> m = {{"a", 1}, {"a", 2}};
Note
Unordered associative containers store their elements in a hash table instead of a tree. Each key is hashed to pick a bucket, so finding, inserting and erasing take O(1) time on average, compared to O(log n) for the tree-based containers.
Views refer to elements owned by another container instead of storing their own:
Name
Description
Category
Example
std::span#include<span>
Contiguous sequence of elements
View
std::span#include<span><int> s(v);
Name
Description
Category
Example
std::flat_set#include<flat_set>
Sorted set of unique keys stored in a sequence container
Adapter
std::flat_set#include<flat_set><int> s = {3, 1, 2};
std::flat_multiset#include<flat_set>
Sorted set that allows duplicate keys, stored in a sequence container
Adapter
std::flat_multiset#include<flat_set><int> s = {1, 1, 2};
std::flat_map
Sorted map with unique keys stored in sequence containers
Adapter
std::flat_map<std::string#include<string>, int> m = {{"a", 1}};
std::flat_multimap
Sorted map that allows duplicate keys, stored in sequence containers
Adapter
std::flat_multimap<std::string#include<string>, int> m = {{"a", 1}, {"a", 2}};
std::mdspan
Multidimensional view of contiguous elements
View
std::mdspan m(v.data(), 2, 3);
Note
Flat containers store their elements in a sorted sequence container (std::vector#include<vector> by default) instead of a tree. Lookups use binary search, and iterating is faster since the elements are contiguous in memory. Inserting and erasing are slower, since later elements have to shift.
Name
Description
Category
Example
std::inplace_vector#include<inplace_vector>
Dynamically sized array with a fixed capacity, stored inside the object
Sequence
std::inplace_vector#include<inplace_vector><int, 8> v = {1, 2, 3};
std::hive
Unordered collection that reuses the memory of erased elements
Sequence
std::hive<int> h = {1, 2, 3};
Methods
The tables below show what methods are available on the STL containers.