[associative.reqmts.general] (original) (raw)
23 Containers library [containers]
23.2 Requirements [container.requirements]
23.2.7 Associative containers [associative.reqmts]
23.2.7.1 General [associative.reqmts.general]
Associative containers provide fast retrieval of data based on keys.
The library provides four basic kinds of associative containers:set,multiset,mapandmultimap.
The library also provides container adaptors that make it easy to construct abstract data types, such as flat_maps, flat_multimaps,flat_sets, or flat_multisets, out of the basic sequence container kinds (or out of other program-defined sequence containers).
Each associative container is parameterized onKeyand an ordering relationComparethat induces a strict weak ordering ([alg.sorting]) on elements ofKey.
In addition,mapandmultimapassociate an arbitrary mapped type Twith theKey.
The object of typeCompareis called thecomparison objectof a container.
The phrase “equivalence of keys” means the equivalence relation imposed by the comparison object.
That is, two keysk1andk2are considered to be equivalent if for the comparison objectcomp,comp(k1, k2) == false && comp(k2, k1) == false.
[Note 1:
This is not necessarily the same as the result of k1 == k2.
— _end note_]
For any two keysk1andk2in the same container, callingcomp(k1, k2)shall always return the same value.
An associative container supports unique keys if it may contain at most one element for each key.
Otherwise, it supports equivalent keys.
The set and map classes support unique keys; the multisetand multimap classes support equivalent keys.
For multiset and multimap,insert, emplace, and erase preserve the relative ordering of equivalent elements.
For set and multiset the value type is the same as the key type.
For map and multimap it is equal to pair<const Key, T>.
iteratorof an associative container is of the bidirectional iterator category.
For associative containers where the value type is the same as the key type, bothiteratorandconst_iteratorare constant iterators.
It is unspecified whether or notiteratorandconst_iteratorare the same type.
[Note 2:
iterator and const_iterator have identical semantics in this case, and iterator is convertible to const_iterator.
Users can avoid violating the one-definition rule by always using const_iterator in their function parameter lists.
— _end note_]
In this subclause,
- X denotes an associative container class,
- a denotes a value of type X,
- a2 denotes a value of a type with nodes compatible with typeX (Table 73),
- b denotes a value of type X or const X,
- u denotes the name of a variable being declared,
- a_uniq denotes a value of type Xwhen X supports unique keys,
- a_eq denotes a value of type Xwhen X supports multiple keys,
- a_tran denotes a value of type X or const Xwhen the qualified-id X::key_compare::is_transparent is valid and denotes a type ([temp.deduct]),
- i and jmeet the Cpp17InputIterator requirements and refer to elements implicitly convertible tovalue_type,
- [i, j) denotes a valid range,
- rg denotes a value of a type Rthat models container-compatible-range<value_type>,
- p denotes a valid constant iterator to a,
- q denotes a valid dereferenceable constant iterator to a,
- r denotes a valid dereferenceable iterator to a,
- [q1, q2) denotes a valid range of constant iterators in a,
- il designates an object of type initializer_list<value_type>,
- t denotes a value of type X::value_type,
- k denotes a value of type X::key_type, and
- c denotes a value of type X::key_compare or const X::key_compare;
- kl is a value such that a is partitioned ([alg.sorting]) with respect to c(x, kl), with x the key value of e and e in a;
- ku is 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 is a value such that a is partitioned with respect toc(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 is 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 iterator or const_iterator; and
- A denotes the storage allocator used by X, if any, or allocator<X::value_type> otherwise,
- m denotes an allocator of a type convertible to A, and nh denotes a non-const rvalue of type X::node_type.
A type X meets the associative container requirements if X meets all the requirements of an allocator-aware container ([container.alloc.reqmts]) and the following types, statements, and expressions are well-formed and have the specified semantics, except that formap and multimap, the requirements placed on value_typein [container.reqmts] apply instead to key_typeand mapped_type.
[Note 3:
For example, in some cases key_type and mapped_typeneed to be Cpp17CopyAssignable even though the associatedvalue_type, pair<const key_type, mapped_type>, is not_Cpp17CopyAssignable_.
— _end note_]
Remarks: For map and multimap only.
Result: Key for set and multiset only;pair<const Key, T> for map and multimap only.
Preconditions: X::value_type is Cpp17Erasable from X.
Preconditions: key_compare is Cpp17CopyConstructible.
typename X::value_compare
Result: A binary predicate type.
It is the same as key_compare for set andmultiset; is an ordering relation on pairs induced by the first component (i.e., Key) for map and multimap.
Result: A specialization of the node-handle class template ([container.node]), such that the public nested types are the same types as the corresponding types in X.
Effects: Constructs an empty container.
Uses a copy of c as a comparison object.
Preconditions: key_compare meets the Cpp17DefaultConstructible requirements.
Effects: Constructs an empty container.
Uses Compare() as a comparison object.
Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from *i.
Effects: Constructs an empty container and inserts elements from the range [i, j) into it; uses c as a comparison object.
Complexity: in general, where N has the value distance(i, j); linear if [i, j) is sorted with respect to value_comp().
Preconditions: key_compare meets the Cpp17DefaultConstructible requirements.
value_type is_Cpp17EmplaceConstructible_ into X from *i.
Effects: Constructs an empty container and inserts elements from the range [i, j) into it; uses Compare() as a comparison object.
Complexity: in general, where N has the value distance(i, j); linear if [i, j) is sorted with respect to value_comp().
Preconditions: value_type is_Cpp17EmplaceConstructible_ into Xfrom *ranges::begin(rg).
Effects: Constructs an empty container and inserts each element from rg into it.
Uses c as the comparison object.
Complexity: in general, where N has the value ranges::distance(rg); linear if rg is sorted with respect to value_comp().
Preconditions: key_compare meets the Cpp17DefaultConstructible requirements.
value_type is Cpp17EmplaceConstructible into Xfrom *ranges::begin(rg).
Effects: Constructs an empty container and inserts each element from rg into it.
Uses Compare() as the comparison object.
Complexity: Same as X(from_range, rg, c).
Effects: Equivalent to X(il.begin(), il.end(), c).
Effects: Equivalent to X(il.begin(), il.end()).
Preconditions: value_type is Cpp17CopyInsertable into Xand Cpp17CopyAssignable.
Effects: Assigns the range [il.begin(), il.end()) into a.
All existing elements of a are either assigned to or destroyed.
Complexity: in general, where N has the value il.size() + a.size(); linear if [il.begin(), il.end()) is sorted with respect to value_comp().
Returns: The comparison object out of which b was constructed.
Result: X::value_compare
Returns: An object of value_compare constructed out of the comparison object.
Result: pair<iterator, bool>
Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from args.
Effects: Inserts a value_type object tconstructed with std::forward<Args>(args)...if and only if there is no element in the container with key equivalent to the key of t.
Returns: The bool component of the returned pair is trueif and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key of t.
Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from args.
Effects: Inserts a value_type object tconstructed with std::forward<Args>(args)....
If a range containing elements equivalent to t exists in a_eq,t is inserted at the end of that range.
Returns: An iterator pointing to the newly inserted element.
Effects: Equivalent to a.emplace(std::forward<Args>(args)...), except that the element is inserted as close as possible to the position just prior to p.
Returns: An iterator pointing to the element with the key equivalent to the newly inserted element.
Complexity: Logarithmic in general, but amortized constant if the element is inserted right before p.
Result: pair<iterator, bool>
Preconditions: If t is a non-const rvalue,value_type is Cpp17MoveInsertable into X; otherwise, value_type is Cpp17CopyInsertable into X.
Effects: Inserts t if and only if there is no element in the container with key equivalent to the key of t.
Returns: The bool component of the returned pair is trueif and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key of t.
Preconditions: If t is a non-const rvalue,value_type is Cpp17MoveInsertable into X; otherwise, value_type is Cpp17CopyInsertable into X.
Effects: Inserts t and returns the iterator pointing to the newly inserted element.
If a range containing elements equivalent to t exists in a_eq,t is inserted at the end of that range.
Preconditions: If t is a non-const rvalue,value_type is Cpp17MoveInsertable into X; otherwise, value_type is Cpp17CopyInsertable into X.
Effects: Inserts t if and only if there is no element with key equivalent to the key of t in containers with unique keys; always inserts t in containers with equivalent keys.
t is inserted as close as possible to the position just prior to p.
Returns: An iterator pointing to the element with key equivalent to the key of t.
Complexity: Logarithmic in general, but amortized constant if t is inserted right before p.
Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from *i.
Neither i nor j are iterators into a.
Effects: Inserts each element from the range [i, j) if and only if there is no element with key equivalent to the key of that element in containers with unique keys; always inserts that element in containers with equivalent keys.
Complexity: , where N has the value distance(i, j).
Preconditions: value_type is_Cpp17EmplaceConstructible_ into Xfrom *ranges::begin(rg).
rg and a do not overlap.
Effects: Inserts each element from rg if and only if there is no element with key equivalent to the key of that element in containers with unique keys; always inserts that element in containers with equivalent keys.
Complexity: , where N has the value ranges::distance(rg).
Effects: Equivalent to a.insert(il.begin(), il.end()).
Result: insert_return_type
Preconditions: nh is empty ora_uniq.get_allocator() == nh.get_allocator() is true.
Effects: If nh is empty, has no effect.
Otherwise, inserts the element owned by nh if and only if there is no element in the container with a key equivalent to nh.key().
Returns: If nh is empty, inserted is false,position is end(), and node is empty.
Otherwise if the insertion took place, inserted is true,position points to the inserted element, and node is empty; if the insertion failed, inserted is false,node has the previous value of nh, andposition points to an element with a key equivalent to nh.key().
Preconditions: nh is empty ora_eq.get_allocator() == nh.get_allocator() is true.
Effects: If nh is empty, has no effect and returns a_eq.end().
Otherwise, inserts the element owned by nh and returns an iterator pointing to the newly inserted element.
If a range containing elements with keys equivalent to nh.key()exists in a_eq, the element is inserted at the end of that range.
Postconditions: nh is empty.
Preconditions: nh is empty ora.get_allocator() == nh.get_allocator() is true.
Effects: If nh is empty, has no effect and returns a.end().
Otherwise, inserts the element owned by nh if and only if there is no element with key equivalent to nh.key()in containers with unique keys; always inserts the element owned by nhin containers with equivalent keys.
The element is inserted as close as possible to the position just prior to p.
Postconditions: nh is empty if insertion succeeds, unchanged if insertion fails.
Returns: An iterator pointing to the element with key equivalent to nh.key().
Complexity: Logarithmic in general, but amortized constant if the element is inserted right before p.
Effects: Removes the first element in the container with key equivalent to k.
Returns: A node_type owning the element if found, otherwise an empty node_type.
Effects: Removes the first element in the container with key rsuch that !c(r, kx) && !c(kx, r) is true.
Returns: A node_type owning the element if found, otherwise an empty node_type.
Effects: Removes the element pointed to by q.
Returns: A node_type owning that element.
Complexity: Amortized constant.
Preconditions: a.get_allocator() == a2.get_allocator() is true.
Effects: Attempts to extract each element in a2 and insert it into ausing the comparison object of a.
In containers with unique keys, if there is an element in a with key equivalent to the key of an element from a2, then that element is not extracted from a2.
Postconditions: Pointers and references to the transferred elements of a2refer to those same elements but as members of a.
If a.begin() and a2.begin() have the same type, iterators referring to the transferred elements will continue to refer to their elements, but they now behave as iterators into a, not into a2.
Throws: Nothing unless the comparison object throws.
Complexity: , where N has the value a2.size().
Effects: Erases all elements in the container with key equivalent to k.
Returns: The number of erased elements.
Effects: Erases all elements in the container with key rsuch that !c(r, kx) && !c(kx, r) is true.
Returns: The number of erased elements.
Effects: Erases the element pointed to by q.
Returns: An iterator pointing to the element immediately following qprior to the element being erased.
If no such element exists, returns a.end().
Complexity: Amortized constant.
Effects: Erases the element pointed to by r.
Returns: An iterator pointing to the element immediately following rprior to the element being erased.
If no such element exists, returns a.end().
Complexity: Amortized constant.
Effects: Erases all the elements in the range [q1, q2).
Returns: An iterator pointing to the element pointed to by q2prior to any elements being erased.
If no such element exists, a.end() is returned.
Complexity: , where N has the value distance(q1, q2).
Effects: Equivalent to a.erase(a.begin(), a.end()).
Postconditions: a.empty() is true.
Complexity: Linear in a.size().
Result: iterator; const_iterator for constant b.
Returns: An iterator pointing to an element with the key equivalent to k, orb.end() if such an element is not found.
Result: iterator; const_iterator for constant a_tran.
Returns: An iterator pointing to an element with key rsuch that !c(r, ke) && !c(ke, r) is true, ora_tran.end() if such an element is not found.
Returns: The number of elements with key equivalent to k.
Returns: The number of elements with key rsuch that !c(r, ke) && !c(ke, r).
Effects: Equivalent to: return b.find(k) != b.end();
Effects: Equivalent to: return a_tran.find(ke) != a_tran.end();
Result: iterator; const_iterator for constant b.
Returns: An iterator pointing to the first element with key not less than k, or b.end() if such an element is not found.
Result: iterator; const_iterator for constant a_tran.
Returns: An iterator pointing to the first element with key rsuch that !c(r, kl), or a_tran.end() if such an element is not found.
Result: iterator; const_iterator for constant b.
Returns: An iterator pointing to the first element with key greater than k, or b.end() if such an element is not found.
Result: iterator; const_iterator for constant a_tran.
Returns: An iterator pointing to the first element with key rsuch that c(ku, r), or a_tran.end() if such an element is not found.
Result: pair<iterator, iterator>;pair<const_iterator, const_iterator> for constant b.
Effects: Equivalent to: return make_pair(b.lower_bound(k), b.upper_bound(k));
Result: pair<iterator, iterator>;pair<const_iterator, const_iterator> for constant a_tran.
Effects: Equivalent to:return make_pair(a_tran.lower_bound(ke), a_tran.upper_bound(ke));
The insert, insert_range, and emplace members shall not affect the validity of iterators and references to the container, and the erase members shall invalidate only iterators and references to the erased elements.
The extract members invalidate only iterators to the removed element; pointers and references to the removed element remain valid.
However, accessing the element through such pointers and references while the element is owned by a node_type is undefined behavior.
References and pointers to an element obtained while it is owned by a node_type are invalidated if the element is successfully inserted.
The fundamental property of iterators of associative containers is that they iterate through the containers in the non-descending order of keys where non-descending is defined by the comparison that was used to construct them.
For any two dereferenceable iteratorsiandjsuch that distance fromitojis positive, the following condition holds:value_comp(*j, *i) == false
For associative containers with unique keys the stronger condition holds:value_comp(*i, *j) != false
When an associative container is constructed by passing a comparison object the container shall not store a pointer or reference to the passed object, even if that object is passed by reference.
When an associative container is copied, through either a copy constructor or an assignment operator, the target container shall then use the comparison object from the container being copied, as if that comparison object had been passed to the target container in its constructor.
The member function templatesfind, count, contains,lower_bound, upper_bound, equal_range,erase, and extractshall not participate in overload resolution unless the qualified-id Compare::is_transparent is valid and denotes a type ([temp.deduct]).
Additionally, the member function templates extract and eraseshall not participate in overload resolution ifis_convertible_v<K&&, iterator> || is_convertible_v<K&&, const_iterator>is true, where K is the type substituted as the first template argument.
A deduction guide for an associative container shall not participate in overload resolution if any of the following are true:
- It has an InputIterator template parameter and a type that does not qualify as an input iterator is deduced for that parameter.
- It has an Allocator template parameter and a type that does not qualify as an allocator is deduced for that parameter.
- It has a Compare template parameter and a type that qualifies as an allocator is deduced for that parameter.