The Containers library is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures like queues, lists and stacks. There are three classes of containers -- sequence containers, associative containers, and unordered associative containers -- each of which is designed to support a different set of operations.
The container manages the storage space that is allocated for its elements and provides member functions to access them, either directly or through iterators (objects with similar properties to pointers).
Most containers have at least several member functions in common, and share functionalities. Which container is the best for the particular application depends not only on the offered functionality, but also on its efficiency for different workloads.
Sequence containers
Sequence containers implement data structures which can be accessed sequentially.
| static contiguous array (class template) |
| dynamic contiguous array (class template) |
| double-ended queue (class template) |
| singly-linked list (class template) |
| doubly-linked list (class template) |
Associative containers
Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity).
| collection of unique keys, sorted by keys (class template) |
| collection of key-value pairs, sorted by keys, keys are unique (class template) |
| collection of keys, sorted by keys (class template) |
| collection of key-value pairs, sorted by keys (class template) |
Unordered associative containers
Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) amortized, O(n) worst-case complexity).
| collection of unique keys, hashed by keys (class template) |
| collection of key-value pairs, hashed by keys, keys are unique (class template) |
| collection of keys, hashed by keys (class template) |
| collection of key-value pairs, hashed by keys (class template) |
Container adaptors
Container adaptors provide a different interface for sequential containers.
| adapts a container to provide stack (LIFO data structure) (class template) |
| adapts a container to provide queue (FIFO data structure) (class template) |
| adapts a container to provide priority queue (class template) |
Iterator invalidation
Thread safety - All container functions can be called concurrently by different threads on different containers. More generally, the C++ standard library functions do not read objects accessible by other threads unless those objects are directly or indirectly accessible via the function arguments, including the this pointer.
- All
const member functions can be called concurrently by different threads on the same container. In addition, the member functions begin() , end() , rbegin() , rend() , front() , back() , data() , find() , lower_bound() , upper_bound() , equal_range() , at() , and, except in associative containers, operator[] , behave as const for the purposes of thread safety (that is, they can also be called concurrently by different threads on the same container). More generally, the C++ standard library functions do not modify objects unless those objects are accessible, directly or indirectly, via the function's non-const arguments, including the this pointer. - Different elements in the same container can be modified concurrently by different threads, except for the elements of
std::vector<bool> (for example, a vector of std::future objects can be receiving values from multiple threads). - Iterator operations (e.g. incrementing an iterator) read, but do not modify the underlying container, and may be executed concurrently with operations on other iterators on the same container, with the const member functions, or reads from the elements. Container operations that invalidate any iterators modify the container and cannot be executed concurrently with any operations on existing iterators even if those iterators are not invalidated.
- Elements of the same container can be modified concurrently with those member functions that are not specified to access these elements. More generally, the C++ standard library functions do not read objects indirectly accessible through their arguments (including other elements of a container) except when required by its specification.
- In any case, container operations (as well as algorithms, or any other C++ standard library functions) may be parallelized internally as long as this does not change the user-visible results (e.g.
std::transform may be parallelized, but not std::for_each which is specified to visit each element of a sequence in order)
| (since C++11) |
Member function table
| - functions present in C++03 |
| - functions present since C++11 |
(implicit)
(implicit)
(implicit)
Iterators
Element
access
Capacity
Modifiers
List operations
Lookup
Observers
Allocator
Container
Sequence containers Associative containers Unordered associative containers Container adaptors
(A PDF version of this table is available at File:container-library-overview-2012-12-27.pdf.).
Please login to continue.