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 ([container.requirements]), 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 Hash 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.
A hash function is a function object that takes a single argument of type Key and returns a value of type std::size_t.
Two values k1 and k2 of type Key are considered equivalent if the container's key_equal function object returns true when passed those values. If k1 and k2 are equivalent, the hash function shall return the same value for both. [ Note: Thus, when an unordered associative container is instantiated with a non-default Pred parameter it usually needs a non-default Hash parameter as well. — end note ]
An unordered associative container supports unique keys if it may contain at most one element for each key. Otherwise, it supports equivalent keys. unordered_set and unordered_map support unique keys. unordered_multiset and unordered_multimap support 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_set and unordered_multiset the value type is the same as the key type. For unordered_map and unordered_multimap it is std::pair<const Key, T>.
The elements of an unordered associative container are organized into buckets. 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 ([container.requirements.general]), except that for unordered_map and unordered_multimap, the requirements placed on value_type in Table [tab:containers.container.requirements] apply instead to key_type and mapped_type. [ Note: For example, key_type and mapped_type are sometimes required to be CopyAssignable even though the associated value_type, pair<const key_type, mapped_type>, is not CopyAssignable. — end note ]
In table [tab:HashRequirements]: X is an unordered associative container class, a is an object of type X, b is a possibly const object of type X, a_uniq is an object of type X when X supports unique keys, a_eq is an object of type X when X supports equivalent keys, i and j are input iterators that refer to value_type, [i, j) is a valid range, p and q2 are valid const iterators to a, q and q1 are valid dereferenceable const iterators to a, [q1, q2) is a valid range in a, il designates an object of type initializer_list<value_type>, t is a value of type X::value_type, k is a value of type key_type, hf is a possibly const value of type hasher, eq is a possibly const value of type key_equal, n is a value of type size_type, and z is a value of type float.
Expression | Return type | Assertion/note pre-/post-condition | Complexity |
X::key_type | Key | Requires: Key shall be Destructible. | compile time |
X::mapped_type (unordered_map and unordered_multimap only) | T | Requires: T is Destructible. | compile time |
X::hasher | Hash | Hash shall be a unary function object type such that the expression hf(k) has type std::size_t. | compile time |
X::key_equal | Pred | Pred shall be a binary predicate that takes two arguments of type Key. Pred is an equivalence relation. | compile time |
X::local_iterator | 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 |
X::const_local_iterator | 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 |
X(n, hf, eq) X a(n, hf, eq) | X | Requires: hasher and key_equal are CopyConstructible. Effects: Constructs an empty container with at least n buckets, using hf as the hash function and eq as the key equality predicate. | Ο(n) |
X(n, hf) X a(n, hf) | X | Requires: hasher is CopyConstructible and key_equal
is DefaultConstructible. Effects: Constructs an empty container with at least n buckets, using hf as the hash function and key_equal() as the key equality predicate. | Ο(n) |
X(n) X a(n) | X | Requires: hasher and key_equal are DefaultConstructible. Effects: Constructs an empty container with at least n buckets, using hasher() as the hash function and key_equal() as the key equality predicate. | Ο(n) |
X() X a | X | Requires: hasher and key_equal are DefaultConstructible. 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. | constant |
X(i, j, n, hf, eq) X a(i, j, n, hf, eq) | X | Requires: hasher and key_equal are CopyConstructible.
value_type is EmplaceConstructible into X from *i. Effects: Constructs an empty container with at least n buckets, using hf as the hash function and eq as the key equality predicate, and inserts elements from [i, j) into it. | Average case Ο(N) (N is distance(i, j)), worst case Ο(N^{2}) |
X(i, j, n, hf) X a(i, j, n, hf) | X | Requires: hasher is CopyConstructible and key_equal
is DefaultConstructible.
value_type is EmplaceConstructible into X from *i. 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) (N is distance(i, j)), worst case Ο(N^{2}) |
X(i, j, n) X a(i, j, n) | X | Requires: hasher and key_equal are DefaultConstructible.
value_type is EmplaceConstructible into X from *i. 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) (N is distance(i, j)), worst case Ο(N^{2}) |
X(i, j) X a(i, j) | X | Requires: hasher and key_equal are DefaultConstructible.
value_type is EmplaceConstructible into X from *i. 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) (N is distance(i, j)), worst case Ο(N^{2}) |
X(il) | X | Same as X(il.begin(), il.end()). | Same as X(il.begin(), il.end()). |
X(b) X a(b) | X | Copy constructor. In addition to the requirements of Table [tab:containers.container.requirements], copies the hash function, predicate, and maximum load factor. | Average case linear in b.size(), worst case quadratic. |
a = b | X& | Copy assignment operator. In addition to the requirements of Table [tab:containers.container.requirements], copies the hash function, predicate, and maximum load factor. | Average case linear in b.size(), worst case quadratic. |
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. | Same as a = X(il). |
b.hash_function() | hasher | Returns b's hash function. | constant |
b.key_eq() | key_equal | Returns b's key equality predicate. | 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. |
Average case Ο(1), worst case Ο(a_uniq. size()). |
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. |
Average case Ο(1), worst case Ο(a_eq. size()). |
a.emplace_hint(p, args) | iterator |
Requires: value_type shall be EmplaceConstructible into X from args. Effects: 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 const_iterator p is a hint pointing to where the search should start. Implementations are permitted to ignore the hint. | Average case Ο(1), worst case Ο(a. size()). |
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 indicates whether the insertion takes place, and the iterator component points to the element with key equivalent to the key of t. | Average case Ο(1), worst case Ο(a_uniq. size()). |
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 an iterator pointing to the newly inserted element. | Average case Ο(1), worst case Ο(a_eq. size()). |
a.insert(q, 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: Equivalent to a.insert(t). Return value is an iterator pointing to the element with the key equivalent to that of t. The iterator q is a hint pointing to where the search should start. Implementations are permitted to ignore the hint. | Average case Ο(1), worst case Ο(a.size()). |
a.insert(i, j) | void | Requires: value_type shall be EmplaceConstructible into X from *i. Pre: i and j are not iterators in a. Equivalent to a.insert(t) for each element in [i,j). | Average case Ο(N), where N is distance(i, j). Worst
case Ο(N * (a.size()) + N). |
a.insert(il) | void | Same as a.insert(il.begin(), il.end()). | Same as a.insert( il.begin(), il.end()). |
a.erase(k) | size_type | Erases all elements with key equivalent to k. Returns the number of elements erased. | Average case Ο(a.count(k)). Worst case Ο(a.size()). |
a.erase(q) | iterator | Erases the element pointed to by q. Return value is the iterator immediately following q prior to the erasure. | Average case Ο(1), worst case Ο(a.size()). |
a.erase(q1, q2) | iterator | Erases all elements in the range [q1, q2). Return value is the iterator immediately following the erased elements prior to the erasure. | Average case linear in distance(q1, q2), worst case Ο(a.size()). |
a.clear() | void | Erases all elements in the container. Post: a.empty() returns true | Linear. |
b.find(k) | iterator; const_iterator for const b. | Returns an iterator pointing to an element with key equivalent to k, or b.end() if no such element exists. | Average case Ο(1), worst case Ο(b.size()). |
b.count(k) | size_type | Returns the number of elements with key equivalent to k. | Average case Ο(1), worst case Ο(b.size()). |
b.equal_range(k) | pair<iterator, iterator>; pair<const_iterator, const_iterator> for const b. | Returns a range containing all elements with keys equivalent to k. Returns make_pair(b.end(), b.end()) if no such elements exist. | Average case Ο(b.count(k)). Worst case Ο(b.size()). |
b.bucket_count() | size_type | Returns the number of buckets that b contains. | Constant |
b.max_bucket_count() | size_type | Returns an upper bound on the number of buckets that b might ever contain. | Constant |
b.bucket(k) | size_type |
Pre: b.bucket_count() > 0. Returns the index of the bucket in which elements with keys equivalent to k would be found, if any such element existed. Post: the return value shall be in the range [0, b.bucket_count()). | Constant |
b.bucket_size(n) | size_type | Pre: n shall be in the range [0, b.bucket_count()). Returns the number of elements in the n^{ th} bucket. | Ο(b.bucket_size(n)) |
b.begin(n) | local_iterator; const_local_iterator for const b. | Pre: n shall be in the range [0, b.bucket_count()). b.begin(n) returns an iterator referring to the first element in the bucket. If the bucket is empty, then b.begin(n) == b.end(n). | Constant |
b.end(n) | local_iterator; const_local_iterator for const b. | Pre: n shall be in the range [0, b.bucket_count()). b.end(n) returns an iterator which is the past-the-end value for the bucket. | Constant |
b.cbegin(n) | const_local_iterator | Pre: n shall be in the range [0, b.bucket_count()). Note: [b.cbegin(n), b.cend(n)) is a valid range containing all of the elements in the n^{ th} bucket. | Constant |
b.cend(n) | const_local_iterator | Pre: n shall be in the range [0, b.bucket_count()). | Constant |
b.load_factor() | float | Returns the average number of elements per bucket. | Constant |
b.max_load_factor() | float | Returns a positive number that the container attempts to keep the load factor less than or equal to. The container automatically increases the number of buckets as necessary to keep the load factor below this number. | Constant |
a.max_load_factor(z) | void | Pre: z shall be positive. May change the container's maximum load load factor, using z as a hint. | Constant |
a.rehash(n) | void | Post: a.bucket_count() > a.size() / a.max_load_factor() and a.bucket_count() >= n. | Average case linear in a.size(), worst case quadratic. |
a.reserve(n) | void | Same as a.rehash(ceil(n / a.max_load_factor())). | 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 distance(Ea1, Ea2) == distance(Eb1, Eb2) and is_permutation(Ea1, Ea2, Eb1) 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_equal(), and to the hasher returned by hash_function()) is proportional to N in the average case and to N^{2} 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 N^{2} in the worst case, where N is a.size(), and E_{i} is the size of the i^{th} equivalent-key group in a. However, if the respective elements of each corresponding pair of equivalent-key groups Ea_{i} and Eb_{i} 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 Ο(N^{2}), e.g., for a pathologically bad hash function). The behavior of a program that uses operator== or operator!= on unordered containers is undefined unless the Hash and Pred function objects respectively have the same behavior for both containers and the equality comparison operator for Key is a refinement264 of the partition into equivalent-key groups produced by Pred.
The iterator types iterator and const_iterator of an unordered associative container are of at least the forward iterator category. For unordered associative containers where the key type and value type are the same, both iterator and const_iterator are const iterators.
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.
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.
Equality comparison is a refinement of partitioning if no two objects that compare equal fall into different partitions.
For unordered associative containers, no clear() function throws an exception. erase(k) does not throw an exception unless that exception is thrown by the container's Hash or Pred object (if any).
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).