[unord.req.general] (original) (raw)

23 Containers library [containers]

23.2 Requirements [container.requirements]

23.2.8 Unordered associative containers [unord.req]

23.2.8.1 General [unord.req.general]

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

Unordered associative containers conform to the requirements for Containers ([container.requirements]), except that the expressionsa == 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 Predthat 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 byhash — is called the hash function of the container.

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

Two values k1 and k2 are considered equivalent if the container's key equality predicatepred(k1, k2) is valid and returnstrue when passed those values.

If k1 andk2 are equivalent, the container's hash function shall return the same value for both.

[Note 1:

Thus, when an unordered associative container is instantiated with a non-default Pred parameter it usually needs a non-default Hashparameter as well.

— _end note_]

For any two keys k1 and k2 in the same container, calling pred(k1, k2) shall always return the same value.

For any key k in a container, calling hash(k)shall always return the same value.

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_mapsupport unique keys.

unordered_multiset and unordered_multimapsupport 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 andunordered_multimap it is pair<const Key, T>.

For unordered 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 andconst_iterator are the same type.

[Note 2:

iterator and const_iterator have identical semantics in this case, and iterator is convertible toconst_iterator.

Users can avoid violating the one-definition rule by always using const_iterator in their function parameter lists.

— _end note_]

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 andunordered_multimap, rehashing preserves the relative ordering of equivalent elements.

In this subclause,

A type X meets the unordered 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 for unordered_map and unordered_multimap, the requirements placed on value_type in [container.reqmts]apply instead to key_type and mapped_type.

[Note 3:

For example, key_type and mapped_typesometimes need to be _Cpp17CopyAssignable_even though the associated value_type,pair<const key_type, mapped_type>, is not Cpp17CopyAssignable.

— _end note_]

Remarks: For unordered_map and unordered_multimap only.

Result: Key for unordered_set and unordered_multiset only;pair<const Key, T>for unordered_map and unordered_multimap only.

Preconditions: value_type is Cpp17Erasable from X.

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

Preconditions: Pred meets the Cpp17CopyConstructible requirements.

Pred is a binary predicate that takes two arguments of type Key.

Pred is an equivalence relation.

typename X::local_iterator

Result: An iterator type whose category, value type, difference type, and pointer and reference types are the same as X​::​iterator's.

[Note 4:

A local_iterator object can be used to iterate through a single bucket, but cannot be used to iterate across buckets.

— _end note_]

typename X::const_local_iterator

Result: An iterator type whose category, value type, difference type, and pointer and reference types are the same as X​::​const_iterator's.

[Note 5:

A const_local_iterator object can be used to iterate through a single bucket, but cannot be used to iterate across buckets.

— _end note_]

Result: A specialization of a 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 with at least n buckets, using hf as the hash function andeq as the key equality predicate.

Preconditions: key_equal meets the Cpp17DefaultConstructible requirements.

Effects: Constructs an empty container with at least n buckets, using hf as the hash function andkey_equal() as the key equality predicate.

Preconditions: hasher and key_equalmeet the Cpp17DefaultConstructible requirements.

Effects: Constructs an empty container with at least n buckets, using hasher() as the hash function andkey_equal() as the key equality predicate.

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

Effects: Constructs an empty container with an unspecified number of buckets, using hasher() as the hash function andkey_equal() as the key equality predicate.

Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from *i.

Effects: Constructs an empty container with at least n buckets, using hf as the hash function andeq as the key equality predicate, and inserts elements from [i, j) into it.

Complexity: Average case (N is distance(i, j)), worst case .

Preconditions: key_equal meets the Cpp17DefaultConstructible requirements.

value_type is_Cpp17EmplaceConstructible_ into X from *i.

Effects: Constructs an empty container with at least n buckets, using hf as the hash function andkey_equal() as the key equality predicate, and inserts elements from [i, j) into it.

Complexity: Average case (N is distance(i, j)), worst case .

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type is_Cpp17EmplaceConstructible_ into X from *i.

Effects: Constructs an empty container with at least n buckets, using hasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements from [i, j) into it.

Complexity: Average case (N is distance(i, j)), worst case .

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type is_Cpp17EmplaceConstructible_ into X from *i.

Effects: Constructs an empty container with an unspecified number of buckets, using hasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements from [i, j) into it.

Complexity: Average case (N is distance(i, j)), worst case .

X(from_range, rg, n, hf, eq)

Preconditions: value_type is_Cpp17EmplaceConstructible_ into Xfrom *ranges​::​begin(rg).

Effects: Constructs an empty container with at least n buckets, using hf as the hash function andeq as the key equality predicate, and inserts elements from rg into it.

Complexity: Average case (N is ranges​::​distance(rg)), worst case .

Preconditions: key_equal meets the Cpp17DefaultConstructible requirements.

value_type is_Cpp17EmplaceConstructible_ into Xfrom *ranges​::​begin(rg).

Effects: Constructs an empty container with at least n buckets, using hf as the hash function andkey_equal() as the key equality predicate, and inserts elements from rg into it.

Complexity: Average case (N is ranges​::​distance(rg)), worst case .

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type is_Cpp17EmplaceConstructible_ into Xfrom *ranges​::​begin(rg).

Effects: Constructs an empty container with at least n buckets, using hasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements from rg into it.

Complexity: Average case (N is ranges​::​distance(rg)), worst case .

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type is_Cpp17EmplaceConstructible_ into Xfrom *ranges​::​begin(rg).

Effects: Constructs an empty container with an unspecified number of buckets, using hasher() as the hash function andkey_equal() as the key equality predicate, and inserts elements from rg into it.

Complexity: Average case (N is ranges​::​distance(rg)), worst case .

Effects: Equivalent to X(il.begin(), il.end()).

Effects: Equivalent to X(il.begin(), il.end(), n).

Effects: Equivalent to X(il.begin(), il.end(), n, hf).

Effects: Equivalent to X(il.begin(), il.end(), n, hf, eq).

Effects: In addition to the container requirements ([container.reqmts]), copies the hash function, predicate, and maximum load factor.

Complexity: Average case linear in b.size(), worst case quadratic.

Effects: In addition to the container requirements, copies the hash function, predicate, and maximum load factor.

Complexity: Average case linear in b.size(), worst case quadratic.

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: Average case linear in il.size(), worst case quadratic.

Returns: b's hash function.

Returns: b's key equality predicate.

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.

Complexity: Average case , worst case .

Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from args.

Effects: Inserts a value_type object tconstructed with std​::​forward<Args>(args)....

Returns: An iterator pointing to the newly inserted element.

Complexity: Average case , worst case .

Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from args.

Effects: Equivalent to a.emplace(std​::​forward<Args>(args)...).

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

Complexity: Average case , worst case .

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

Complexity: Average case , worst case .

Preconditions: If t is a non-const rvalue,value_type is Cpp17MoveInsertable into X; otherwise, value_type is Cpp17CopyInsertable into X.

Returns: An iterator pointing to the newly inserted element.

Complexity: Average case , worst case .

Preconditions: If t is a non-const rvalue,value_type is Cpp17MoveInsertable into X; otherwise, value_type is Cpp17CopyInsertable into X.

Effects: Equivalent to a.insert(t).

The iterator p is a hint pointing to where the search should start.

Implementations are permitted to ignore the hint.

Returns: An iterator pointing to the element with the key equivalent to that of t.

Complexity: Average case , worst case .

Preconditions: value_type is_Cpp17EmplaceConstructible_ into X from *i.

Neither i nor j are iterators into a.

Effects: Equivalent to a.insert(t) for each element in [i, j).

Complexity: Average case , where N is distance(i, j), worst case .

Preconditions: value_type is_Cpp17EmplaceConstructible_ into Xfrom *ranges​::​begin(rg).

rg and a do not overlap.

Effects: Equivalent to a.insert(t) for each element t in rg.

Complexity: Average case , where N is ranges​::​distance(rg), worst case .

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

Postconditions: 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, and positionpoints to an element with a key equivalent to nh.key().

Complexity: Average case , worst case .

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.

Postconditions: nh is empty.

Complexity: Average case , worst case .

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 iterator q is a hint pointing to where the search should start.

Implementations are permitted to ignore the hint.

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: Average case , worst case .

Effects: Removes an element in the container with key equivalent to k.

Returns: A node_type owning the element if found, otherwise an empty node_type.

Complexity: Average case , worst case .

Effects: Removes an element in the container with key equivalent to kx.

Returns: A node_type owning the element if found, otherwise an empty node_type.

Complexity: Average case , worst case .

Effects: Removes the element pointed to by q.

Returns: A node_type owning that element.

Complexity: Average case , worst case .

Preconditions: a.get_allocator() == a2.get_allocator().

Effects: Attempts to extract each element in a2 and insert it into ausing the hash function and key equality predicate of a.

In containers with unique keys, if there is an element in awith 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.

Complexity: Average case , where N is a2.size(), worst case .

Effects: Erases all elements with key equivalent to k.

Returns: The number of elements erased.

Complexity: Average case , worst case .

Effects: Erases all elements with key equivalent to kx.

Returns: The number of elements erased.

Complexity: Average case , worst case .

Effects: Erases the element pointed to by q.

Returns: The iterator immediately following q prior to the erasure.

Complexity: Average case , worst case .

Effects: Erases the element pointed to by r.

Returns: The iterator immediately following r prior to the erasure.

Complexity: Average case , worst case .

Effects: Erases all elements in the range [q1, q2).

Returns: The iterator immediately following the erased elements prior to the erasure.

Complexity: Average case linear in distance(q1, q2), worst case .

Effects: Erases all elements in the container.

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 key equivalent to k, orb.end() if no such element exists.

Complexity: Average case , worst case .

Result: iterator; const_iterator for constant a_tran.

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

Complexity: Average case , worst case .

Returns: The number of elements with key equivalent to k.

Complexity: Average case , worst case .

Returns: The number of elements with key equivalent to ke.

Complexity: Average case , worst case .

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

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

Result: pair<iterator, iterator>;pair<const_iterator, const_iterator> for constant b.

Returns: A range containing all elements with keys equivalent to k.

Returns make_pair(b.end(), b.end()) if no such elements exist.

Complexity: Average case , worst case .

Result: pair<iterator, iterator>;pair<const_iterator, const_iterator> for constant a_tran.

Returns: A range containing all elements with keys equivalent to ke.

Returns make_pair(a_tran.end(), a_tran.end()) if no such elements exist.

Complexity: Average case , worst case .

Returns: The number of buckets that b contains.

Returns: An upper bound on the number of buckets that b can ever contain.

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

The return value is in the range [0, b.bucket_count()).

Preconditions: a_tran.bucket_count() > 0.

Postconditions: The return value is in the range [0, a_tran.bucket_count()).

Returns: The index of the bucket in which elements with keys equivalent to ke would be found, if any such element existed.

Preconditions: n shall be in the range [0, b.bucket_count()).

Returns: The number of elements in the bucket.

Result: local_iterator; const_local_iterator for constant b.

Preconditions: n is in the range [0, b.bucket_count()).

Returns: An iterator referring to the first element in the bucket.

If the bucket is empty, then b.begin(n) == b.end(n).

Result: local_iterator; const_local_iterator for constant b.

Preconditions: n is in the range [0, b.bucket_count()).

Returns: An iterator which is the past-the-end value for the bucket.

Result: const_local_iterator

Preconditions: n shall be in the range [0, b.bucket_count()).

Returns: An iterator referring to the first element in the bucket.

If the bucket is empty, then b.cbegin(n) == b.cend(n).

Result: const_local_iterator

Preconditions: n is in the range [0, b.bucket_count()).

Returns: An iterator which is the past-the-end value for the bucket.

Returns: The average number of elements per bucket.

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.

Preconditions: z is positive.

May change the container's maximum load factor, using z as a hint.

Postconditions: a.bucket_count() >= a.size() / a.max_load_factor() anda.bucket_count() >= n.

Complexity: Average case linear in a.size(), worst case quadratic.

Effects: Equivalent to a.rehash(ceil(n / a.max_load_factor())).

Two unordered containers a and b compare equal ifa.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 thatis_permutation(Ea1, Ea2, Eb1, Eb2) returns true.

Forunordered_set and unordered_map, the complexity ofoperator== (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 toN in the average case and to in the worst case, where N isa.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 forunordered_multiset and unordered_multimap becomes proportional to N (but worst-case complexity remains , e.g., for a pathologically bad hash function).

The behavior of a program that usesoperator== or operator!= on unordered containers is undefined unless the Pred function object has the same behavior for both containers and the equality comparison function for Key is a refinement195of 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 andconst_iterator are constant iterators.

The insert, insert_range, 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, insert_range, 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, andz 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 anode_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 templatesfind, count, equal_range, contains,extract, erase, and bucketshall not participate in overload resolution unless the qualified-ids Pred​::​is_transparent andHash​::​is_transparentare both valid and denote types ([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 unordered associative container shall not participate in overload resolution if any of the following are true: