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.
NameDescriptionCategoryExample
std::vector#include <vector>Dynamically sized arraySequencestd::vector#include <vector><int> v(3, 0);
std::dequeDouble-ended queueSequencestd::deque<int> d(3, 0);
std::listDoubly linked listSequencestd::list<int> l(3, 0);
std::string#include <string>Dynamically sized array of charactersSequencestd::string#include <string> s("abc");
std::set#include <set>Sorted collection of unique elementAssociativestd::set#include <set><int> s;
std::multiset#include <set>Sorted collection that allows duplicate elementsAssociativestd::multiset#include <set><int> s;
std::map#include <map>Sorted key-value pairs with unique keysAssociativestd::map#include <map><std::string#include <string>, int> m;
std::multimapSorted key-value pairs that allows duplicate keysAssociativestd::multimap<std::string#include <string>, int> m;
std::stackLast in, first outAdapterstd::stack<int> s;
std::queueFirst in, first outAdapterstd::queue<int> q;
std::priority_queueLargest (or prioritised) element firstAdapterstd::priority_queue<int> q;
NameDescriptionCategoryExample
std::array#include <array>Fixed-size arraySequencestd::array#include <array><int, 3> a = {1, 2, 3};
std::forward_listSingly linked listSequencestd::forward_list<int> l = {1, 2, 3};
std::unordered_set#include <unordered_set>Hashed collection of unique elementsUnordered associativestd::unordered_set#include <unordered_set><int> s = {1, 2, 3};
std::unordered_multiset#include <unordered_set>Hashed collection that allows duplicate keysUnordered associativestd::unordered_multiset#include <unordered_set><int> s = {1, 1, 2};
std::unordered_mapHashed key-value pairs with unique keysUnordered associativestd::unordered_map<std::string#include <string>, int> m = {{"a", 1}};
std::unordered_multimapHashed key-value pairs that allows duplicate keysUnordered associativestd::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:

NameDescriptionCategoryExample
std::span#include <span>Contiguous sequence of elementsViewstd::span#include <span><int> s(v);
NameDescriptionCategoryExample
std::flat_set#include <flat_set>Sorted set of unique keys stored in a sequence containerAdapterstd::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 containerAdapterstd::flat_multiset#include <flat_set><int> s = {1, 1, 2};
std::flat_mapSorted map with unique keys stored in sequence containersAdapterstd::flat_map<std::string#include <string>, int> m = {{"a", 1}};
std::flat_multimapSorted map that allows duplicate keys, stored in sequence containersAdapterstd::flat_multimap<std::string#include <string>, int> m = {{"a", 1}, {"a", 2}};
std::mdspanMultidimensional view of contiguous elementsViewstd::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.

NameDescriptionCategoryExample
std::inplace_vector#include <inplace_vector>Dynamically sized array with a fixed capacity, stored inside the objectSequencestd::inplace_vector#include <inplace_vector><int, 8> v = {1, 2, 3};
std::hiveUnordered collection that reuses the memory of erased elementsSequencestd::hive<int> h = {1, 2, 3};

Methods

The tables below show what methods are available on the STL containers.

Containersize()empty()clear()begin(), end()[]at()front()back()push_back()pop_back()push_front(), pop_front()insert(), erase()find()count()push(), pop()top()
std::vector#include <vector>✓✓✓✓✓✓✓✓✓✓✓
std::deque✓✓✓✓✓✓✓✓✓✓✓✓
std::list✓✓✓✓✓✓✓✓✓✓
std::string#include <string>✓✓✓✓✓✓✓✓✓
std::set#include <set>, std::multiset#include <set>✓✓✓✓✓✓✓
std::map#include <map>✓✓✓✓✓✓✓✓
std::multimap✓✓✓✓✓✓✓
std::stack, std::priority_queue✓✓✓✓
std::queue✓✓✓✓✓
Containersize()empty()clear()begin(), end()[]at()front()back()push_front(), pop_front()insert(), erase()insert_after(), erase_after()find()count()
std::array#include <array>✓✓✓✓✓✓✓
std::forward_list✓✓✓✓✓✓
std::unordered_set#include <unordered_set>, std::unordered_multiset#include <unordered_set>✓✓✓✓✓✓✓
std::unordered_map✓✓✓✓✓✓✓✓✓
std::unordered_multimap✓✓✓✓✓✓✓
Containersize()empty()begin(), end()[]front()back()
std::span#include <span>✓✓✓✓✓✓
Containersize()empty()clear()begin(), end()[]at()insert(), erase()find()count()
std::flat_set#include <flat_set>, std::flat_multiset#include <flat_set>✓✓✓✓✓✓✓
std::flat_map✓✓✓✓✓✓✓✓✓
std::flat_multimap✓✓✓✓✓✓✓
std::mdspan✓✓✓
Containersize()empty()clear()begin(), end()[]at()front()back()push_back()pop_back()insert(), erase()
std::inplace_vector#include <inplace_vector>✓✓✓✓✓✓✓✓✓✓✓
std::hive✓✓✓✓✓

Time Complexity

The tables below show the time complexity of container operations.

ContainerRandom accessFindInsert/remove at frontInsert/remove at backInsert/remove elsewhere
std::vector#include <vector>O(1)O(n)O(n)O(1) amortisedO(n)
std::dequeO(1)O(n)O(1)O(1)O(n)
std::listO(n)O(1)O(1)O(1)
std::string#include <string>O(1)O(n)O(n)O(1) amortisedO(n)
std::set#include <set>, std::map#include <map>, std::multiset#include <set>, std::multimapO(log n)O(log n)
std::stackO(1)
std::queueO(1)O(1)
std::priority_queueO(log n)
ContainerRandom accessFindInsert/remove at frontInsert/remove elsewhere
std::array#include <array>O(1)O(n)
std::forward_listO(n)O(1)O(1)
std::unordered_set#include <unordered_set>, std::unordered_map, std::unordered_multiset#include <unordered_set>, std::unordered_multimapO(1) averageO(1) average
ContainerRandom accessFind
std::span#include <span>O(1)O(n)
ContainerRandom accessFindInsert/remove
std::flat_set#include <flat_set>, std::flat_map, std::flat_multiset#include <flat_set>, std::flat_multimapO(log n)O(n)
std::mdspanO(1)
ContainerRandom accessFindInsert/remove at backInsert/remove elsewhere
std::inplace_vector#include <inplace_vector>O(1)O(n)O(1)O(n)
std::hiveO(n)O(1)

References