C++ named requirements: AssociativeContainer
From cppreference.com
An AssociativeContainer is an ordered Container that provides fast lookup of objects based on keys.
An associative container supports unique keys if it may contain at most one element for each key. Otherwise, it supports equivalent keys.
Contents |
[edit] Requirements
The type X satisfies AssociativeContainer if
- The type
Xsatisfies Container (until C++11)AllocatorAwareContainer (since C++11), - is parameterized on
Keyand an ordering relationComparethat induces a strict weak ordering on elements ofKey, and- In addition, std::map and std::multimap associate an arbitrary mapped type
Twith theKey. - The object of type
Compareis called the comparison object of a container of typeX.
- In addition, std::map and std::multimap associate an arbitrary mapped type
Given
- a, a value of type
X - a2, a value of a type
Ywhose node handles are compatible withX - b, a value of type
Xor const X - a_uniq, a value of type
XwhenXsupports unique keys - a_eq, a value of type
XwhenXsupports equivalent keys - a_tran, a value of type
Xor const X when typeX::key_compare::is_transparentexists - i and j, LegacyInputIterators denoting a valid range and referring to elements implicitly convertible to
X::value_type - p, a valid constant iterator to a
- q, a valid dereferenceable constant iterator to a
- r, a valid dereferenceable iterator to a
- q1 and q2, const iterators denoting a valid range in a
- il, an object of type std::initializer_list<X::value_type>
- t, a value of type
X::value_type - k, a value of type
X::key_type - c, a value of type
X::key_compareor const X::key_compare - kl, a value such that a is partitioned with respect to c(x, kl), with x the key value of e and e in a
- ku, a value such that a is partitioned with respect to !c(ku, x), with x the key value of e and e in a
- ke, a value such that a is partitioned with respect to c(x, ke) and !c(ke, x), with c(x, ke) implying !c(ke, x) and with x the key value of e and e in a
- kx, a value such that
- a is partitioned with respect to c(x, kx) and !c(kx, x), with c(x, kx) implying !c(kx, x) and with x the key value of e and e in a, and
- kx is not convertible to either
X::iteratororX::const_iterator
-
A, the allocator type ofX:X::allocator_typeif it exists, otherwise std::allocator<X::value_type> - m, an allocator of a type convertible to
A - nh, a non-const rvalue of type
X::node_type
[edit] Types
| Name | Type | Requirements |
|---|---|---|
key_type |
Key |
|
mapped_type |
T (for std::map and std::multimap only) |
|
value_type |
|
Erasable from X
|
key_compare |
Compare |
CopyConstructible |
value_compare |
|
BinaryPredicate |
node_type |
A specialization of the node-handle class template, such that the public nested types are the same types as the corresponding types in X. |
[edit] Methods and operators
| expression | return type | pre/requirements | post/effects | complexity |
|---|---|---|---|---|
| X(c) | Construct an empty container using a copy of c as the comparison object | constant | ||
| X(), X a = X(); | X::key_compare is DefaultConstructible |
Construct an empty container using a Compare() as the comparison object | constant | |
| X(i, j, c) | X::value_type is EmplaceConstructible into X from *i |
Constructs an empty container using a copy of c as the comparison object and inserts all elements from the range [i, j) |
generally N·log N, or N if [i, j) is sorted (where N is std::distance(i, j))
| |
| X(i, j) | X::key_compare is DefaultConstructible and X::value_type is EmplaceConstructible into X from *i |
Constructs an empty container using a Compare() as the comparison object and inserts all elements from the range [i, j) |
generally N·log N, or N if [i, j) is sorted according to value_comp() (where N is std::distance(i, j))
| |
| X(il); | Equivalent to X(il.begin(), il.end()); |
Equivalent to X(il.begin(), il.end()); | ||
| a = il | X& |
T is CopyInsertable into X and also CopyAssignable |
Assign the range [il.begin(), il.end()) into a. Elements of a that were not assigned to are destroyed |
generally N·log N, or N if [il.begin(), il.end()) is sorted according to value_comp() (where N is il.size() + a.size())
|
| a.key_comp() | X::key_compare |
The comparison object with which a was constructed is returned. | constant | |
| a.value_comp() | X::value_compare |
An object of type X::value_compare constructed out of the comparison object is returned. |
constant |
| This section is incomplete Reason: Finish requirements. |
[edit] Associative containers in the standard library
| collection of unique keys, sorted by keys (class template) | |
| collection of keys, sorted by keys (class template) | |
| collection of key-value pairs, sorted by keys, keys are unique (class template) | |
| collection of key-value pairs, sorted by keys (class template) |
[edit] 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 354 | C++98 | lower_bound and upper_bound did notreturn the end iterator if no element is found |
they return the end iterator in this case |
| LWG 589 | C++98 | the elements that i and j refer to had the type X::value_type
|
the elements are implicitly convertible to X::value_type
|