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 two(until C++11)three(since C++11) classes of 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 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.
[edit] Sequence containersSequence containers implement data structures which can be accessed sequentially.
fixed-sized inplace contiguous arrayAssociative containers implement sorted data structures that can be quickly searched (O(log n) complexity).
collection of unique keys, sorted by keysUnordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) average, O(n) worst-case complexity).
[edit] Container adaptorsContainer adaptors provide a different interface for sequential containers.
adapts a container to provide stack (LIFO data structure)Views provide flexible facilities for interacting with one- or multi-dimensional views over a non-owning array of elements.
a non-owning view over a contiguous sequence of objectsRead-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.
Category Container After insertion, are... After erasure, are... Conditionally iterators valid? references valid? iterators valid? references valid? Sequence containers array N/A N/A vector No N/A Insertion changed capacity Yes Yes Before modified element(s)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.
clear
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::set::end is never invalidated, std::unordered_set::end is invalidated only on rehash(since C++11), std::vector::end 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::deque 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::deque iterators, the net result is that the only modifying operation which does not invalidate std::deque::end is an erasure which deletes the first element, but not the last.
Thread safetybegin()
, 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.Note: std::basic_string 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.
- functions present in C++03 - functions present since C++11 - functions present since C++17 - functions present since C++20 - functions present since C++23 [edit] Member function tableThe <
, <=
, >
, >=
, and !=
operators are synthesized from operator<=> and operator== respectively.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR Applied to Behavior as published Correct behavior LWG 51 C++98 container iterators might be invalidatedC++ named requirements:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4