Difference between revisions of "cpp/container"
m |
(+) |
||
Line 449: | Line 449: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/at }} |
| {{n/a|}} | | {{n/a|}} | ||
|- | |- | ||
Line 472: | Line 472: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/operator_at |operator[] }} |
| {{n/a|}} | | {{n/a|}} | ||
|- | |- | ||
Line 562: | Line 562: | ||
| {{cpp/container/table cell o | /queue/empty }} | | {{cpp/container/table cell o | /queue/empty }} | ||
| {{cpp/container/table cell o | /priority_queue/empty }} | | {{cpp/container/table cell o | /priority_queue/empty }} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/empty }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/empty }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/empty }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/empty }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 585: | Line 585: | ||
| {{cpp/container/table cell o | /queue/size }} | | {{cpp/container/table cell o | /queue/size }} | ||
| {{cpp/container/table cell o | /priority_queue/size }} | | {{cpp/container/table cell o | /priority_queue/size }} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/size }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/size }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/size }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/size }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 608: | Line 608: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/max_size }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/max_size }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/max_size }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/max_size }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 723: | Line 723: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/clear }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/clear}} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/clear }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/clear }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 746: | Line 746: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/insert }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/insert }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/insert }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/insert }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 771: | Line 771: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/insert_or_assign }} |
| {{n/a|}} | | {{n/a|}} | ||
|- | |- | ||
Line 792: | Line 792: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/emplace }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/emplace }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/emplace }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/emplace }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 815: | Line 815: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/emplace_hint }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/emplace_hint }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/emplace_hint }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/emplace_hint }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 840: | Line 840: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/try_emplace }} |
| {{n/a|}} | | {{n/a|}} | ||
|- | |- | ||
Line 861: | Line 861: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_set/erase }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multiset/erase }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_map/erase }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /flat_multimap/erase }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 1,022: | Line 1,022: | ||
| {{cpp/container/table cell n | /queue/swap }} | | {{cpp/container/table cell n | /queue/swap }} | ||
| {{cpp/container/table cell n | /priority_queue/swap }} | | {{cpp/container/table cell n | /priority_queue/swap }} | ||
− | | {{ | + | | {{cpp/container/table cell 23 | /unordered_set/swap }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /unordered_multiset/swap }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /unordered_map/swap }} |
− | | {{ | + | | {{cpp/container/table cell 23 | /unordered_multimap/swap }} |
|- | |- | ||
<!-- --> | <!-- --> | ||
Line 1,068: | Line 1,068: | ||
| {{n/a|}} | | {{n/a|}} | ||
| {{n/a|}} | | {{n/a|}} | ||
− | | {{n/a|}} | + | | {{n/a|}} <!-- note: there is 'extract' but with an entirely different meaning --> |
− | | {{n/a|}} | + | | {{n/a|}} <!-- note: there is 'extract' but with an entirely different meaning --> |
− | | {{n/a|}} | + | | {{n/a|}} <!-- note: there is 'extract' but with an entirely different meaning --> |
− | | {{n/a|}} | + | | {{n/a|}} <!-- note: there is 'extract' but with an entirely different meaning --> |
|- | |- | ||
! rowspan="6" | List operations | ! rowspan="6" | List operations |
Revision as of 07:19, 22 September 2022
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:
- sequence containers,
- associative containers, and
|
(since C++11) |
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.
Contents |
Sequence containers
Sequence containers implement data structures which can be accessed sequentially.
(C++11) |
fixed-sized inplace contiguous array (class template) |
dynamic contiguous array (class template) | |
double-ended queue (class template) | |
(C++11) |
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 (since C++11)
Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) amortized, O(n) worst-case complexity).
(C++11) |
collection of unique keys, hashed by keys (class template) |
(C++11) |
collection of key-value pairs, hashed by keys, keys are unique (class template) |
(C++11) |
collection of keys, hashed by keys (class template) |
(C++11) |
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) | |
(C++23) |
adapts a container to provide a collection of unique keys, sorted by keys (class template) |
(C++23) |
adapts two containers to provide a collection of key-value pairs, sorted by unique keys (class template) |
(C++23) |
adapts a container to provide a collection of keys, sorted by keys (class template) |
(C++23) |
adapts two containers to provide a collection of key-value pairs, sorted by keys (class template) |
Views
span
(since C++20) and mdspan
(since C++23) are non-owning views over a contiguous sequence of objects, the storage of which is owned by some other object.
(C++20) |
a non-owning view over a contiguous sequence of objects (class template) |
(C++23) |
a multi-dimensional non-owning array view (class template) |
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.
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) (for insertion only if capacity didn't change) | ||||
No | No | At or after modified element(s) | ||||
deque | No | Yes | Yes, except erased element(s) | Modified first or last element | ||
No | No | Modified middle only | ||||
list | Yes | Yes, except erased element(s) | ||||
forward_list | Yes | Yes, except erased element(s) | ||||
Associative containers | set multiset map multimap |
Yes | Yes, except erased element(s) | |||
Unordered associative containers | unordered_set unordered_multiset unordered_map unordered_multimap |
No | Yes | N/A | Insertion caused rehash | |
Yes | Yes, except erased element(s) | No rehash |
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::set::insert, std::map::emplace, std::vector::push_back, and std::deque::push_front.
|
(since C++11) |
- Examples of erasure methods are std::set::erase, std::vector::pop_back, std::deque::pop_front, and std::map::clear.
-
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 safety
|
(since C++11) |
Member function table
This section is incomplete Reason: This table is incomplete: add C++20 span, C++23 mdspan, flat_* |
- 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 |
Non-member function table
This section is incomplete Reason: Create similar table for C++20 non-member container functions, e.g. erase_if, etc., and maybe begin/end families too. |
Defect reports
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 invalidated by arbitrary library operation |
they are only invalidated when specified |
See also
C++ named requirements:
- Container
- SequenceContainer
- ContiguousContainer
- ReversibleContainer
- AssociativeContainer
- AllocatorAwareContainer
- UnorderedAssociativeContainer
numeric arrays, array masks and array slices (class template) |