23 Containers library [containers]

23.2 Container requirements [container.requirements]

23.2.4 Associative containers [associative.reqmts]

Associative containers provide fast retrieval of data based on keys. The library provides four basic kinds of associative containers: set, multiset, map and multimap.

Each associative container is parameterized on Key and an ordering relation Compare that induces a strict weak ordering ([alg.sorting]) on elements of Key. In addition, map and multimap associate an arbitrary mapped type T with the Key. The object of type Compare is called the comparison object of a container.

The phrase “equivalence of keys” means the equivalence relation imposed by the comparison and not the operator== on keys. That is, two keys k1 and k2 are considered to be equivalent if for the comparison object comp, comp(k1, k2) == false && comp(k2, k1) == false. For any two keys k1 and k2 in the same container, calling comp(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 multiset and 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>.

iterator of an associative container is of the bidirectional iterator category. For associative containers where the value type is the same as the key type, both iterator and const_iterator are constant iterators. It is unspecified whether or not iterator and const_iterator are the same type. [ Note: 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 ]

The associative containers meet all the requirements of Allocator-aware containers ([container.requirements.general]), except that for map and multimap, the requirements placed on value_type in Table [tab:containers.container.requirements] apply instead to key_type and mapped_type. [ Note: For example, in some cases key_type and mapped_type are required to be CopyAssignable even though the associated value_type, pair<const key_type, mapped_type>, is not CopyAssignable.  — end note ]

In Table [tab:containers.associative.requirements], X denotes an associative container class, a denotes a value of X, a_uniq denotes a value of X when X supports unique keys, a_eq denotes a value of X when X supports multiple keys, a_tran denotes a value of X when the qualified-id X::key_compare::is_transparent is valid and denotes a type ([temp.deduct]), i and j satisfy input iterator requirements and refer to elements implicitly convertible to value_type, [i,j) denotes a valid range, p denotes a valid const iterator to a, q denotes a valid dereferenceable const iterator to a, [q1, q2) denotes a valid range of const iterators in a, il designates an object of type initializer_list<value_type>, t denotes a value of X::value_type, k denotes a value of X::key_type and c denotes a value of type X::key_compare; kl is a value such that a is partitioned ([alg.sorting]) with respect to c(r, kl), with r the key value of e and e in a; ku is a value such that a is partitioned with respect to !c(ku, r); ke is a value such that a is partitioned with respect to c(r, ke) and !c(ke, r), with c(r, ke) implying !c(ke, r). A denotes the storage allocator used by X, if any, or std::allocator<X::value_type> otherwise, and m denotes an allocator of a type convertible to A.

Table 102 — Associative container requirements (in addition to container)
ExpressionReturn typeAssertion/noteComplexity
pre-/post-condition
X::key_type Key compile time
mapped_type (map and multimap only) T compile time
X::value_type (set and multiset only) Key Requires: value_type is Erasable from X compile time
X::value_type (map and multimap only) pair<const Key, T> Requires: value_type is Erasable from X compile time
X::key_compare Compare defaults to less<key_type> compile time
X::value_compare a binary predicate type is the same as key_compare for set and multiset; is an ordering relation on pairs induced by the first component (i.e., Key) for map and multimap. compile time
X(c)
X a(c);
Requires: key_compare is CopyConstructible.
Effects: Constructs an empty container. Uses a copy of c as a comparison object.
constant
X()
X a;
Requires: key_compare is DefaultConstructible.
Effects: Constructs an empty container. Uses Compare() as a comparison object
constant
X(i,j,c)
X a(i,j,c);
Requires: key_compare is CopyConstructible. value_type is EmplaceConstructible 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.
N log N in general (N has the value distance(i, j)); linear if [i, j) is sorted with value_comp()
X(i,j) X a(i,j); Requires: key_compare is DefaultConstructible. value_type is EmplaceConstructible into X from *i.
Effects: Same as above, but uses Compare() as a comparison object
same as above
X(il); Same as X(il.begin(), il.end()). Same as X(il.begin(), il.end()).
X(il,c); Same as X(il.begin(), il.end(), c). Same as X(il.begin(), il.end(), c).
a = il X& Requires: value_type is CopyInsertable into X and CopyAssignable.
Effects: Assigns the range [il.begin(),il.end()) into a. All existing elements of a are either assigned to or destroyed.
N log N in general (where N has the value il.size() + a.size()); linear if [il.begin(),il.end()) is sorted with value_comp().
a.key_comp() X::key_compare returns the comparison object out of which a was constructed. constant
a.value_comp() X::value_compare returns an object of value_compare constructed out of the comparison object constant
a_uniq. emplace(args) pair<iterator, bool> Requires: value_type shall be EmplaceConstructible into X from args.
Effects: Inserts a value_type object t constructed with std::forward<Args>(args)... if and only if there is no element in the container with key equivalent to the key of t. The bool component of the returned pair is true if 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.
logarithmic
a_eq. emplace(args) iterator Requires: value_type shall be EmplaceConstructible into X from args.
Effects: Inserts a value_type object t constructed with std::forward<Args>(args)... 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.
logarithmic
a.emplace_hint(p, args) iterator equivalent to a.emplace( std::forward<Args>(args)...). Return value is an iterator pointing to the element with the key equivalent to the newly inserted element. The element is inserted as close as possible to the position just prior to p. logarithmic in general, but amortized constant if the element is inserted right before p
a_uniq. insert(t) pair<iterator, bool> Requires: If t is a non-const rvalue expression, value_type shall be MoveInsertable into X; otherwise, value_type shall be CopyInsertable into X.
Effects: Inserts t if and only if there is no element in the container with key equivalent to the key of t. The bool component of the returned pair is true if 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.
logarithmic
a_eq.insert(t) iterator Requires: If t is a non-const rvalue expression, value_type shall be MoveInsertable into X; otherwise, value_type shall be CopyInsertable 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.
logarithmic
a.insert(
p, t)
iterator Requires: If t is a non-const rvalue expression, value_type shall be MoveInsertable into X; otherwise, value_type shall be CopyInsertable 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. Always returns the iterator pointing to the element with key equivalent to the key of t. t is inserted as close as possible to the position just prior to p.
logarithmic in general, but amortized constant if t is inserted right before p.
a.insert(
i, j)
void Requires: value_type shall be EmplaceConstructible into X from *i.
pre: i, j are not iterators into a. 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.
Nlog (a.size() + N) (N has the value distance(i, j)
a.insert(il) void Equivalent to a.insert(il.begin(), il.end()).
a.erase(k) size_type erases all elements in the container with key equivalent to k. returns the number of erased elements. log (a.size()) + a.count(k)
a.erase(q) iterator erases the element pointed to by q. Returns an iterator pointing to the element immediately following q prior to the element being erased. If no such element exists, returns a.end(). amortized constant
a.erase(
q1, q2)
iterator erases all the elements in the range [q1,q2). Returns an iterator pointing to the element pointed to by q2 prior to any elements being erased. If no such element exists, a.end() is returned. log (a.size()) + N where N has the value distance(q1, q2).
a.clear() void a.erase(a.begin(),a.end())
post: a.empty() returns true
linear in a.size().
a.find(k) iterator; const_iterator for constant a. returns an iterator pointing to an element with the key equivalent to k, or a.end() if such an element is not found logarithmic
a_tran.
find(ke)
iterator; const_iterator for constant a_tran. returns an iterator pointing to an element with key r such that !c(r, ke) && !c(ke, r), or a_tran.end() if such an element is not found logarithmic
a.count(k) size_type returns the number of elements with key equivalent to k log (a.size()) + a.count(k)
a_tran.
count(ke)
size_type returns the number of elements with key r such that !c(r, ke) && !c(ke, r) log (a_tran.size()) + a_tran.count(ke)
a.lower_bound(k) iterator; const_iterator for constant a. returns an iterator pointing to the first element with key not less than k, or a.end() if such an element is not found. logarithmic
a_tran.
lower_bound(kl)
iterator; const_iterator for constant a_tran. returns an iterator pointing to the first element with key r such that !c(r, kl), or a_tran.end() if such an element is not found. logarithmic
a.upper_bound(k) iterator; const_iterator for constant a. returns an iterator pointing to the first element with key greater than k, or a.end() if such an element is not found. logarithmic
a_tran.
upper_bound(ku)
iterator; const_iterator for constant a_tran. returns an iterator pointing to the first element with key r such that c(ku, r), or a_tran.end() if such an element is not found. logarithmic
a.equal_range(k) pair<iterator, iterator>; pair<const_iterator, const_iterator> for constant a. equivalent to make_pair(a.lower_bound(k), a.upper_bound(k)). logarithmic
a_tran.
equal_range(ke)
pair<iterator, iterator>; pair<const_iterator, const_iterator> for constant a_tran. equivalent to make_pair(
a_tran.lower_bound(ke), a_tran.upper_bound(ke)).
logarithmic

The insert 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 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 iterators i and j such that distance from i to j is positive,

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, either through 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 templates find, count, lower_bound, upper_bound, and equal_range shall not participate in overload resolution unless the qualified-id Compare::is_transparent is valid and denotes a type ([temp.deduct]).

23.2.4.1 Exception safety guarantees [associative.reqmts.except]

For associative containers, no clear() function throws an exception. erase(k) does not throw an exception unless that exception is thrown by the container's Compare object (if any).

For associative containers, if an exception is thrown by any operation from within an insert or emplace function inserting a single element, the insertion has no effect.

For associative containers, no swap function throws an exception unless that exception is thrown by the swap of the container's Compare object (if any).