cpp/container

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 classes of containers:
 * sequence containers,
 * associative containers, and

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 properties similar 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.

Associative containers
Associative containers implement sorted data structures that can be quickly searched ($O(log n)$ complexity).

Unordered associative containers
Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched ($O(1)$ average, $O(n)$ worst-case complexity).

Container adaptors
Container adaptors provide a different interface for sequential containers.

Views
Views provide flexible facilities for interacting with one- or multi-dimensional views over a non-owning array of elements.

Iterator invalidation
Read-only methods never invalidate iterators or references. Methods which modify the contents of a container may invalidate iterators and/or references, as summarized in this table.

Here, insertion refers to any method which adds one or more elements to the container and erasure refers to any method which removes one or more elements from the container.


 * Examples of insertion methods are std, std, std, and std.


 * Examples of erasure methods are std, std, std, and std.
 * invalidates all iterators and references. Because it erases all elements, this technically complies with the rules above.

Unless otherwise specified (either explicitly or by defining a function in terms of other functions), passing a container as an argument to a library function never invalidate iterators to, or change the values of, objects within that container.

The past-the-end iterator deserves particular mention. In general this iterator is invalidated as though it were a normal iterator to a non-erased element. So std is never invalidated, std is always invalidated (since it is always after the modified elements), and so on.

There is one exception: an erasure which deletes the last element of a std does invalidate the past-the-end iterator, even though it is not an erased element of the container (or an element at all). Combined with the general rules for std iterators, the net result is that the only modifying operation which does not invalidate std is an erasure which deletes the first element, but not the last.

{{rrev|since=c++11|

Thread safety
}}
 * 1) 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.
 * 2) All const member functions can be called concurrently by different threads on the same container. In addition, the member functions, , , , , , , , , , , , and, except in associative containers, , 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.
 * 3) Different elements in the same container can be modified concurrently by different threads, except for the elements of std (for example, a vector of std objects can be receiving values from multiple threads).
 * 4) 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.
 * 5) 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.
 * 6) 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 may be parallelized, but not std which is specified to visit each element of a sequence in order).

Function table
Note: std is not treated as a container by the standard but behaves much like one due to its similarity. It is listed as 'Pseudo container' here for convenience.

Member function table

 * Note: functions in two different lines have different meanings and syntax: