Unordered associative containers provide an ability for fast retrieval
of data based on keys.

The worst-case complexity for most operations
is linear, but the average case is much faster.

The library provides
four unordered associative containers: unordered_set,
unordered_map, unordered_multiset, and
unordered_multimap.

Unordered associative containers conform to the requirements for
Containers, except that
the expressions
a == b and a != b have different semantics than for the other
container types.

Each unordered associative container is parameterized by Key,
by a function object type Hash that meets the *Cpp17Hash*
requirements ([hash.requirements]) and acts as a hash function for
argument values of type Key, and by a binary predicate Pred
that induces an equivalence relation on values of type Key.

Additionally, unordered_map and unordered_multimap associate
an arbitrary *mapped type* T with the Key.

The container's object of type Hash — denoted by
hash — is called the *hash function* of the
container.

The container's object of type Pred —
denoted by pred — is called the
*key equality predicate* of the container.

An unordered associative container supports *unique keys* if it
may contain at most one element for each key.

Otherwise, it supports
*equivalent keys*.

In containers that support equivalent keys,
elements with equivalent keys are adjacent to each other
in the iteration order of the container.

Thus, although the absolute order
of elements in an unordered container is not specified, its elements are
grouped into *equivalent-key groups* such that all elements of each
group have equivalent keys.

Mutating operations on unordered containers shall
preserve the relative order of elements within each equivalent-key group
unless otherwise specified.

For unordered containers where the value type is the same as the key
type, both iterator and const_iterator are constant
iterators.

Keys with the same hash code appear in the same
bucket.

The number of buckets is automatically increased as elements
are added to an unordered associative container, so that the average
number of elements per bucket is kept below a bound.

Rehashing
invalidates iterators, changes ordering between elements, and changes
which buckets elements appear in, but does not invalidate pointers or
references to elements.

For unordered_multiset and
unordered_multimap, rehashing preserves the relative ordering of
equivalent elements.

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

In Table 83:

- X denotes an unordered 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,
- a_uniq denotes a value of type X when X supports unique keys,
- a_eq denotes a value of type X when X supports equivalent keys,
- a_tran denotes a possibly const value of type X
when the
*qualified-id**s*X::key_equal::is_transparent and X::hasher::is_transparent are both valid and denote types ([temp.deduct]), - i and j denote input iterators that refer to value_type,
- [i, j) denotes a valid range,
- p and q2 denote valid constant iterators to a,
- q and q1 denote valid dereferenceable constant iterators to a,
- r denotes a valid dereferenceable iterator to a,
- [q1, q2) denotes a valid range in a,
- il denotes a value of type initializer_list<value_type>,
- t denotes a value of type X::value_type,
- k denotes a value of type key_type,
- hf denotes a possibly const value of type hasher,
- eq denotes a possibly const value of type key_equal,
- ke is a value such that
- eq(r1, ke) == eq(ke, r1)
- hf(r1) == hf(ke) if eq(r1, ke) is true, and
- (eq(r1, ke) && eq(r1, r2)) == eq(r2, ke)

- n denotes a value of type size_type,
- z denotes a value of type float, and
- nh denotes a non-const rvalue of type X::node_type.

Table 83: Unordered associative container requirements (in addition to container) [tab:container.hash.req]

Expression | Return type | Assertion/note | Complexity | |

pre-/post-condition | ||||

Key | compile time | |||

T | compile time | |||

Key | Preconditions: value_type is Cpp17Erasable from X | compile time | ||

X::value_type (unordered_map and unordered_multimap only) | pair<const Key, T> | Preconditions: value_type is Cpp17Erasable from X | compile time | |

Hash | Preconditions: Hash is a unary function object type such that the expression
hf(k) has type size_t. | compile time | ||

Pred | compile time | |||

An iterator type whose category, value type,
difference type, and pointer and reference types are the same as
X::iterator's. | A local_iterator object may be used to iterate through a
single bucket, but may not be used to iterate across
buckets. | compile time | ||

An iterator type whose category, value type,
difference type, and pointer and reference types are the same as
X::const_iterator's. | A const_local_iterator object may be used to iterate through a
single bucket, but may not be used to iterate across
buckets. | compile time | ||

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 | Effects: Constructs an empty container with at least n buckets,
using hf as the hash function and eq as the key
equality predicate. | |||

X(n, hf) X a(n, hf); | X | |||

X(n) X a(n); | X | |||

X() X a; | X | constant | ||

X(i, j, n, hf, eq) X a(i, j, n, hf, eq); | X | Average case (N is distance(i, j)), worst case
| ||

X(i, j, n, hf) X a(i, j, n, hf); | X | Effects: Constructs an empty container with at least n buckets,
using hf as the hash function and key_equal() as the key
equality predicate, and inserts elements from [i, j) into it. | Average case (N is distance(i, j)), worst case
| |

X(i, j, n) X a(i, j, n); | X | Effects: Constructs an empty container with at least n buckets,
using hasher() as the hash function and key_equal()
as the key equality predicate, and inserts elements from [i, j)
into it. | Average case (N is distance(i, j)), worst case
| |

X(i, j) X a(i, j); | X | Effects: Constructs an empty container with an unspecified number of
buckets, using hasher() as the hash function and
key_equal() as the key equality predicate, and inserts elements
from [i, j) into it. | Average case (N is distance(i, j)), worst case
| |

X(il) | X | Same as X(il.begin(), il.end()). | ||

X(il, n) | X | Same as X(il.begin(), il.end(), n). | ||

X(il, n, hf) | X | Same as X(il.begin(), il.end(), n, hf). | ||

X(il, n, hf, eq) | X | Same as X(il.begin(), il.end(), n, hf, eq). | ||

X(b) X a(b); | X | Average case linear in b.size(), worst case quadratic. | ||

a = b | X& | Average case linear in b.size(), worst case quadratic. | ||

a = il | X& | Same as a = X(il). | ||

hasher | constant | |||

key_equal | constant | |||

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

a_eq.emplace(args) | iterator | |||

iterator | ||||

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.The bool
component of the returned pair indicates whether the insertion
takes place, and the iterator component points to the element
with key equivalent to the key of t. | |||

a_eq.insert(t) | iterator | |||

a.insert(p, t) | iterator | Preconditions: If t is a non-const rvalue, value_type is
Cpp17MoveInsertable into X; otherwise, value_type is
Cpp17CopyInsertable into X.Return value is an iterator pointing
to the element with the key equivalent to that of t. The
iterator p is a hint pointing to where the search should
start. Implementations are permitted to ignore the hint. | ||

a.insert(i, j) | void | |||

a.insert(il) | void | Same as 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(). | ||

a_eq. insert(nh) | iterator | |||

a.insert(q, 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 iterator q is a hint
pointing to where the search should start. Implementations are permitted
to ignore the hint. | ||

node_type | ||||

a.extract(q) | node_type | |||

void | Attempts to extract each element in a2 and insert it into a using the hash function and key equality predicate 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 a2
refer to those same elements but as members of a.Iterators referring
to the transferred elements and all iterators referring to a will
be invalidated, but iterators to elements remaining in a2 will
remain valid. | |||

size_type | ||||

a.erase(q) | iterator | |||

a.erase(r) | iterator | |||

a.erase(q1, q2) | iterator | |||

void | Effects: Erases all elements in the container.Postconditions: a.empty() is true | Linear in a.size(). | ||

Returns: An iterator pointing to an element with key equivalent to
k, or b.end() if no such element exists. | ||||

a_tran.find(ke) | Returns: An iterator pointing to an element with key equivalent to
ke, or a_tran.end() if no such element exists. | |||

size_type | ||||

a_tran.count(ke) | size_type | |||

bool | Effects: Equivalent to b.find(k) != b.end() | |||

a_tran.contains(ke) | bool | Effects: Equivalent to a_tran.find(ke) != a_tran.end() | ||

a_tran.equal_range(ke) | ||||

size_type | Constant | |||

size_type | Constant | |||

size_type | Constant | |||

size_type | ||||

Constant | ||||

Constant | ||||

const_local_iterator | Constant | |||

const_local_iterator | Constant | |||

float | Returns: The average number of elements per bucket. | Constant | ||

float | Constant | |||

a.max_load_factor(z) | void | Constant | ||

void | Average case linear in a.size(), worst case quadratic. | |||

void | Average case linear in a.size(), worst case quadratic. |

Two unordered containers a and b compare equal if
a.size() == b.size() and, for every equivalent-key group
[Ea1, Ea2) obtained from a.equal_range(Ea1), there exists an
equivalent-key group [Eb1, Eb2) obtained from b.equal_range(Ea1),
such that
is_permutation(Ea1, Ea2, Eb1, Eb2) returns true.

For
unordered_set and unordered_map, the complexity of
operator== (i.e., the number of calls to the == operator
of the value_type, to the predicate returned by key_eq(),
and to the hasher returned by hash_function()) is proportional to
N in the average case and to in the worst case, where N is
a.size().

For unordered_multiset and unordered_multimap,
the complexity of operator== is proportional to
in the average case and to in the worst case, where N is a.size(),
and is the size of the equivalent-key group in a.

However, if the respective elements of each corresponding pair of
equivalent-key groups and are arranged in the same order
(as is commonly the case, e.g., if a and b are unmodified copies
of the same container), then the average-case complexity for
unordered_multiset and unordered_multimap becomes
proportional to N (but worst-case complexity remains , e.g., for
a pathologically bad hash function).

The insert and emplace members shall not affect the validity of references to
container elements, but may invalidate all iterators to the
container.

The erase members shall invalidate only iterators and
references to the erased elements, and preserve the relative order of the
elements that are not erased.

The insert and emplace members shall not affect the validity of iterators if
(N+n) <= z * B, where N is the number of elements in
the container prior to the insert operation, n is the
number of elements inserted, B is the container's bucket count, and
z is the container's maximum load factor.

The extract members invalidate only iterators to the removed element,
and preserve the relative order of the elements that are not erased; 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 member function templates
find, count, equal_range, and contains
shall not participate in overload resolution unless
the *qualified-id**s*
Pred::is_transparent and
Hash::is_transparent
are both valid and denote types ([temp.deduct]).

A deduction guide for an unordered 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 Hash template parameter and an integral type or a type that qualifies as an allocator is deduced for that parameter.
- It has a Pred template parameter and a type that qualifies as an allocator is deduced for that parameter.

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

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