Sequence container (C++)
| C++ Standard Library |
|---|
| Containers |
| C standard library |
In C++, sequence containers or sequence collections refer to a group of container class templates in the standard library that implement storage of data elements. Being templates, they can be used to store arbitrary elements, such as integers or custom classes. One common property of all sequential containers is that the elements can be accessed sequentially. Like all other standard library components, they reside in namespace std.
The following containers are defined in the current revision of the C++ standard:
std::array<T, N>implements a compile-time non-resizable array.std::bitset<N>implements a compile-time bit set.std::vector<T>implements an array with fast random access and an ability to automatically resize when appending elements.std::inplace_vector<T>implements a resizable array with contiguous in-place storage.std::deque<T>implements a double-ended queue with comparatively fast random access.std::list<T>implements a doubly linked list.std::forward_list<T>implements a singly linked list.std::hive<T>implements an object pool.
Each of these containers implements different algorithms for data storage, which means that they have different speed guarantees for different operations.
There are also versions of these collections in namespace std::pmr (for polymorphic memory resources). These versions specify the optional template parameter Allocator as std::pmr::polymorphic_allocator.
C++ further defines container adapters that wrap a sequence container:
std::stack<T>implements a stack withdequeas the default underlying container.std::queue<T>implements a queue withdequeas the default underlying container.std::priority_queue<T>implements a priority queue (by default max heap) withvectoras the default underlying container.std::flat_set<T>implements a "flat set" (stores a sorted set of unique keys) withvectoras the default underlying container.std::flat_map<K, V>implements a "flat map" (stores a sorted set of unique key-value pairs) withvectoras the default underlying containers.std::flat_multiset<T>implements a "flat multi-set" (stores a sorted set of keys) withvectoras the default underlying container.std::flat_multimap<K, V>implements a "flat multi-map" (stores a sorted set of key-value pairs) withvectoras the default underlying containers.
The "flat" collections (std::flat_set, std::flat_map, std::flat_multiset, and std::flat_multimap) use std::vector as the underlying container, unlike their "non-flat" equivalents (std::set, std::map, std::multiset, and std::multimap), which store data as red–black trees (hence being "flat").
View container types represent views over non-owning arrays of elements:
std::span<T>implements a non-owning view over a contiguous sequence of objects.std::mdspan<T>implements a non-owning view over a multi-dimensional array.
Since each of the containers needs to be able to copy its elements in order to function properly, the type of the elements must fulfill CopyConstructible and Assignable requirements. For a given container, all elements must belong to the same type. For instance, one cannot store data in the form of both char and int within the same container instance.