Associative containers provide fast retrieval of data based on keys.

Each associative container is parameterized on
Key
and an ordering relation
Compare
that induces a strict weak ordering on
elements of
Key.

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.

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.

The associative containers meet all the requirements of
Allocator-aware containers, except that for
map and multimap, the requirements placed on value_type
in Table 78 apply instead to key_type
and mapped_type.

In Table 82,
X denotes an associative container class,
a denotes a value of type X,
a2 denotes a value of a type with nodes compatible with type
X (Table 81),
b denotes a possibly const value of type X,
u denotes the name of a variable being declared,
a_uniq denotes a value of type X
when X supports unique keys,
a_eq denotes a value of type X
when X supports multiple keys,
a_tran denotes a possibly const value of type 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 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 possibly const 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 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.

Table 82 — Associative container requirements (in addition to container)

Expression | Return type | Assertion/note | Complexity |

pre-/post-condition | |||

X::key_type | Key | compile time | |

X::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 | 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::node_type | a specialization of a node_handle
class template, such that the public nested types are
the same types as the corresponding types in X. | see [container.node] | compile time |

X(c) X u(c); | Effects: Constructs an empty container. Uses a copy of c as a comparison object. | constant | |

X() X u; | Uses Compare() as a comparison object | constant | |

X(i,j,c) X u(i,j,c); | Effects: Constructs an empty container and inserts elements from the range [i, j) into it; uses c as a comparison object. | in general, where N has the value distance(i, j);
linear if [i, j) is sorted with value_comp() | |

X(i,j) X u(i,j); | 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& | All
existing elements of a are either assigned to or destroyed. | in general, where N has the value il.size() + a.size();
linear if [il.begin(), il.end()) is sorted with value_comp() |

b.key_comp() | X::key_compare | returns the comparison object out of which b was constructed. | constant |

b.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> | 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 | 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 | 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. 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. | |

a.insert(i, j) | void | 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. | , where N has the value distance(i, j) |

a.insert(il) | void | equivalent to a.insert(il.begin(), il.end()) | |

a_uniq.insert(nh) | insert_return_type | 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(). 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, and position
points to an element with a key equivalent to nh.key(). | logarithmic |

a_eq.insert(nh) | iterator | 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. | logarithmic |

a.insert(p, nh) | iterator | 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 nh in containers
with equivalent keys. Always returns the iterator pointing to the element
with key equivalent to nh.key(). 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.extract(k) | node_type | removes the first element in the container with key equivalent to k. | log(a.size()) |

a.extract(q) | node_type | removes the element pointed to by q. Returns a node_type owning that element. | amortized constant |

a.merge(a2) | void | 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 a2 refer to those same elements but as members of a. Iterators referring
to the transferred elements will continue to refer to their elements, but
they now behave as iterators into a, not into a2. | |

a.erase(k) | size_type | erases all elements in the container with key equivalent to
k. returns the number of erased elements. | |

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(r) | iterator | erases the element pointed to by r. Returns an iterator pointing to
the element immediately following r 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. | |

a.clear() | void | linear in a.size(). | |

b.find(k) | returns an iterator pointing to an element with the key equivalent
to k, or b.end() if such an element is not found | logarithmic | |

a_tran. find(ke) | 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 | |

b.count(k) | size_type | returns the number of elements with key equivalent to k | |

a_tran. count(ke) | size_type | returns the number of elements with key r such that
!c(r, ke) && !c(ke, r) | |

b.lower_bound(k) | returns an iterator pointing to the first element with
key not less than k,
or b.end() if such an element is not found. | logarithmic | |

a_tran. lower_bound(kl) | 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 | |

b.upper_bound(k) | returns an iterator pointing to the first element with
key greater than k,
or b.end() if such an element is not found. | logarithmic | |

a_tran. upper_bound(ku) | 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 | |

b.equal_range(k) | equivalent to make_pair(b.lower_bound(k), b.upper_bound(k)). | logarithmic | |

a_tran. equal_range(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 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 iterators
i
and
j
such that distance from
i
to
j
is 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, 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]).

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.

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.