Files
2025-10-25 03:02:53 +03:00

76 KiB
Raw Permalink Blame History

[unord.req]

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]

1

#

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.

2

#

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.

3

#

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.

4

#

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.

5

#

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

6

#

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

Otherwise, it supportsequivalent 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.

7

#

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

8

#

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]

9

#

The elements of an unordered associative container are organized intobuckets.

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.

10

#

In this subclause,

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 75),

b denotes a value of type X or const 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 value of type X or const X when the qualified-idsX::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,

rg denotes a value of a type R that models container-compatible-range<value_type>,

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 value of type hasher or const hasher,

eq denotes a value of type key_equal or const 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

if any two of eq(r1, ke), eq(r2, ke), and eq(r1, r2) are true, then all three are true,

where r1 and r2 are keys of elements in a_tran,

kx is a value such that

eq(r1, kx) == eq(kx, r1),

hf(r1) == hf(kx) if eq(r1, kx) is true,

if any two of eq(r1, kx), eq(r2, kx), and eq(r1, r2) are true, then all three are true, and

kx is not convertible to either iterator or const_iterator,

where r1 and r2 are keys of elements in a_tran,

n denotes a value of type size_type,

z denotes a value of type float, and

nh denotes an rvalue of type X::node_type.

11

#

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_type sometimes need to be Cpp17CopyAssignable even though the associated value_type,pair<const key_type, mapped_type>, is not Cpp17CopyAssignable.

— end note]

🔗

typename X::key_type

12

#

Result: Key.

🔗

typename X::mapped_type

13

#

Result: T.

14

#

Remarks: For unordered_map and unordered_multimap only.

🔗

typename X::value_type

15

#

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

16

#

Preconditions: value_type is Cpp17Erasable from X.

🔗

typename X::hasher

17

#

Result: Hash.

18

#

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

🔗

typename X::key_equal

19

#

Result: Pred.

20

#

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

21

#

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

22

#

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]

🔗

typename X::node_type

23

#

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.

🔗

X(n, hf, eq)

24

#

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

25

#

Complexity: O(n)

🔗

X(n, hf)

26

#

Preconditions: key_equal meets the Cpp17DefaultConstructible requirements.

27

#

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

28

#

Complexity: O(n)

🔗

X(n)

29

#

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

30

#

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

31

#

Complexity: O(n)

🔗

X a = X(); X a;

32

#

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

33

#

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

34

#

Complexity: Constant.

🔗

X(i, j, n, hf, eq)

35

#

Preconditions: value_type isCpp17EmplaceConstructible into X from *i.

36

#

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.

37

#

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

🔗

X(i, j, n, hf)

38

#

Preconditions: key_equal meets the Cpp17DefaultConstructible requirements.

value_type isCpp17EmplaceConstructible into X from *i.

39

#

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.

40

#

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

🔗

X(i, j, n)

41

#

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type isCpp17EmplaceConstructible into X from *i.

42

#

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.

43

#

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

🔗

X(i, j)

44

#

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type isCpp17EmplaceConstructible into X from *i.

45

#

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.

46

#

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

🔗

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

47

#

Preconditions: value_type isCpp17EmplaceConstructible into X from *ranges::begin(rg).

48

#

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.

49

#

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

🔗

X(from_range, rg, n, hf)

50

#

Preconditions: key_equal meets the Cpp17DefaultConstructible requirements.

value_type isCpp17EmplaceConstructible into X from *ranges::begin(rg).

51

#

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.

52

#

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

🔗

X(from_range, rg, n)

53

#

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type isCpp17EmplaceConstructible into X from *ranges::begin(rg).

54

#

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.

55

#

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

🔗

X(from_range, rg)

56

#

Preconditions: hasher and key_equal meet the Cpp17DefaultConstructible requirements.

value_type isCpp17EmplaceConstructible into X from *ranges::begin(rg).

57

#

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.

58

#

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

🔗

X(il)

59

#

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

🔗

X(il, n)

60

#

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

🔗

X(il, n, hf)

61

#

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

🔗

X(il, n, hf, eq)

62

#

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

🔗

X(b)

63

#

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

64

#

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

🔗

a = b

65

#

Result: X&

66

#

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

67

#

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

🔗

a = il

68

#

Result: X&

69

#

Preconditions: value_type is Cpp17CopyInsertable into X and Cpp17CopyAssignable.

70

#

Effects: Assigns the range [il.begin(), il.end()) into a.

All existing elements of a are either assigned to or destroyed.

71

#

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

🔗

b.hash_function()

72

#

Result: hasher

73

#

Returns: b's hash function.

74

#

Complexity: Constant.

🔗

b.key_eq()

75

#

Result: key_equal

76

#

Returns: b's key equality predicate.

77

#

Complexity: Constant.

🔗

a_uniq.emplace(args)

78

#

Result: pair<iterator, bool>

79

#

Preconditions: value_type isCpp17EmplaceConstructible into X from args.

80

#

Effects: Inserts a value_type object t constructed with std::forward(args)... if and only if there is no element in the container with key equivalent to the key of t.

81

#

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

82

#

Complexity: Average case O(1), worst case O(a_uniq.size()).

🔗

a_eq.emplace(args)

83

#

Result: iterator

84

#

Preconditions: value_type isCpp17EmplaceConstructible into X from args.

85

#

Effects: Inserts a value_type object t constructed with std::forward(args)....

86

#

Returns: An iterator pointing to the newly inserted element.

87

#

Complexity: Average case O(1), worst case O(a_eq.size()).

🔗

a.emplace_hint(p, args)

88

#

Result: iterator

89

#

Effects: Equivalent to a.emplace(std::forward(args)...), except that the const_iterator p is a hint pointing to where the search should start.

Implementations are permitted to ignore the hint.

90

#

Returns: The iterator returned by emplace.

🔗

a_uniq.insert(t)

91

#

Result: pair<iterator, bool>

92

#

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

93

#

Effects: Inserts t if and only if there is no element in the container with key equivalent to the key of t.

94

#

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.

95

#

Complexity: Average case O(1), worst case O(a_uniq.size()).

🔗

a_eq.insert(t)

96

#

Result: iterator

97

#

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

98

#

Effects: Inserts t.

99

#

Returns: An iterator pointing to the newly inserted element.

100

#

Complexity: Average case O(1), worst case O(a_eq.size()).

🔗

a.insert(p, t)

101

#

Result: iterator

102

#

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

103

#

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.

104

#

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

105

#

Complexity: Average case O(1), worst case O(a.size()).

🔗

a.insert(i, j)

106

#

Result: void

107

#

Preconditions: value_type isCpp17EmplaceConstructible into X from *i.

Neither i nor j are iterators into a.

108

#

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

109

#

Complexity: Average case O(N), where N is distance(i, j), worst case O(N(a.size()+1)).

🔗

a.insert_range(rg)

110

#

Result: void

111

#

Preconditions: value_type isCpp17EmplaceConstructible into X from *ranges::begin(rg).

rg and a do not overlap.

112

#

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

113

#

Complexity: Average case O(N), where N is ranges::distance(rg), worst case O(N(a.size()+1)).

🔗

a.insert(il)

114

#

Effects: Equivalent to a.insert(il.begin(), il.end()).

🔗

a_uniq.insert(nh)

115

#

Result: insert_return_type

116

#

Preconditions: nh is empty ora_uniq.get_allocator() == nh.get_allocator() is true.

117

#

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

118

#

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 position points to an element with a key equivalent to nh.key().

119

#

Complexity: Average case O(1), worst case O(a_uniq.size()).

🔗

a_eq.insert(nh)

120

#

Result: iterator

121

#

Preconditions: nh is empty ora_eq.get_allocator() == nh.get_allocator() is true.

122

#

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.

123

#

Postconditions: nh is empty.

124

#

Complexity: Average case O(1), worst case O(a_eq.size()).

🔗

a.insert(q, nh)

125

#

Result: iterator

126

#

Preconditions: nh is empty ora.get_allocator() == nh.get_allocator() is true.

127

#

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 nh in containers with equivalent keys.

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

Implementations are permitted to ignore the hint.

128

#

Postconditions: nh is empty if insertion succeeds, unchanged if insertion fails.

129

#

Returns: An iterator pointing to the element with key equivalent to nh.key().

130

#

Complexity: Average case O(1), worst case O(a.size()).

🔗

a.extract(k)

131

#

Result: node_type

132

#

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

133

#

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

134

#

Complexity: Average case O(1), worst case O(a.size()).

🔗

a_tran.extract(kx)

135

#

Result: node_type

136

#

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

137

#

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

138

#

Complexity: Average case O(1), worst case O(a_tran.size()).

🔗

a.extract(q)

139

#

Result: node_type

140

#

Effects: Removes the element pointed to by q.

141

#

Returns: A node_type owning that element.

142

#

Complexity: Average case O(1), worst case O(a.size()).

🔗

a.merge(a2)

143

#

Result: void

144

#

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

145

#

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

146

#

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.

147

#

Complexity: Average case O(N), where N is a2.size(), worst case O(N*a.size() + N).

🔗

a.erase(k)

148

#

Result: size_type

149

#

Effects: Erases all elements with key equivalent to k.

150

#

Returns: The number of elements erased.

151

#

Complexity: Average case O(a.count(k)), worst case O(a.size()).

🔗

a_tran.erase(kx)

152

#

Result: size_type

153

#

Effects: Erases all elements with key equivalent to kx.

154

#

Returns: The number of elements erased.

155

#

Complexity: Average case O(a_tran.count(kx)), worst case O(a_tran.size()).

🔗

a.erase(q)

156

#

Result: iterator

157

#

Effects: Erases the element pointed to by q.

158

#

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

159

#

Complexity: Average case O(1), worst case O(a.size()).

🔗

a.erase(r)

160

#

Result: iterator

161

#

Effects: Erases the element pointed to by r.

162

#

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

163

#

Complexity: Average case O(1), worst case O(a.size()).

🔗

a.erase(q1, q2)

164

#

Result: iterator

165

#

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

166

#

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

167

#

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

🔗

a.clear()

168

#

Result: void

169

#

Effects: Erases all elements in the container.

170

#

Postconditions: a.empty() is true.

171

#

Complexity: Linear in a.size().

🔗

b.find(k)

172

#

Result: iterator; const_iterator for constant b.

173

#

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

174

#

Complexity: Average case O(1), worst case O(b.size()).

🔗

a_tran.find(ke)

175

#

Result: iterator; const_iterator for constant a_tran.

176

#

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

177

#

Complexity: Average case O(1), worst case O(a_tran.size()).

🔗

b.count(k)

178

#

Result: size_type

179

#

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

180

#

Complexity: Average case O(b.count(k)), worst case O(b.size()).

🔗

a_tran.count(ke)

181

#

Result: size_type

182

#

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

183

#

Complexity: Average case O(a_tran.count(ke)), worst case O(a_tran.size()).

🔗

b.contains(k)

184

#

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

🔗

a_tran.contains(ke)

185

#

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

🔗

b.equal_range(k)

186

#

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

187

#

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

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

188

#

Complexity: Average case O(b.count(k)), worst case O(b.size()).

🔗

a_tran.equal_range(ke)

189

#

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

190

#

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.

191

#

Complexity: Average case O(a_tran.count(ke)), worst case O(a_tran.size()).

🔗

b.bucket_count()

192

#

Result: size_type

193

#

Returns: The number of buckets that b contains.

194

#

Complexity: Constant.

🔗

b.max_bucket_count()

195

#

Result: size_type

196

#

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

197

#

Complexity: Constant.

🔗

b.bucket(k)

198

#

Result: size_type

199

#

Preconditions: b.bucket_count() > 0.

200

#

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

201

#

Complexity: Constant.

🔗

a_tran.bucket(ke)

202

#

Result: size_type

203

#

Preconditions: a_tran.bucket_count() > 0.

204

#

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

205

#

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

206

#

Complexity: Constant.

🔗

b.bucket_size(n)

207

#

Result: size_type

208

#

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

209

#

Returns: The number of elements in the nth bucket.

210

#

Complexity: O(b.bucket_size(n))

🔗

b.begin(n)

211

#

Result: local_iterator; const_local_iterator for constant b.

212

#

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

213

#

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

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

214

#

Complexity: Constant.

🔗

b.end(n)

215

#

Result: local_iterator; const_local_iterator for constant b.

216

#

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

217

#

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

218

#

Complexity: Constant.

🔗

b.cbegin(n)

219

#

Result: const_local_iterator

220

#

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

221

#

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

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

222

#

Complexity: Constant.

🔗

b.cend(n)

223

#

Result: const_local_iterator

224

#

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

225

#

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

226

#

Complexity: Constant.

🔗

b.load_factor()

227

#

Result: float

228

#

Returns: The average number of elements per bucket.

229

#

Complexity: Constant.

🔗

b.max_load_factor()

230

#

Result: float

231

#

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.

232

#

Complexity: Constant.

🔗

a.max_load_factor(z)

233

#

Result: void

234

#

Preconditions: z is positive.

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

235

#

Complexity: Constant.

🔗

a.rehash(n)

236

#

Result: void

237

#

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

238

#

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

🔗

a.reserve(n)

239

#

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

240

#

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 N2 in the worst case, where N isa.size().

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

However, if the respective elements of each corresponding pair of equivalent-key groups Eai and Ebi 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 O(N2), 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 refinement194 of the partition into equivalent-key groups produced by Pred.

241

#

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.

242

#

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.

243

#

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.

244

#

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.

245

#

The member function templatesfind, count, equal_range, contains,extract, erase, and bucket shall not participate in overload resolution unless the qualified-idsPred::is_transparent andHash::is_transparent are both valid and denote types ([temp.deduct]).

Additionally, the member function templates extract and erase shall 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.

246

#

A deduction guide for an unordered associative container shall not participate in overload resolution if any of the following are true:

  • (246.1)

    It has an InputIterator template parameter and a type that does not qualify as an input iterator is deduced for that parameter.

  • (246.2)

    It has an Allocator template parameter and a type that does not qualify as an allocator is deduced for that parameter.

  • (246.3)

    It has a Hash template parameter and an integral type or a type that qualifies as an allocator is deduced for that parameter.

  • (246.4)

    It has a Pred template parameter and a type that qualifies as an allocator is deduced for that parameter.

194)194)

Equality comparison is a refinement of partitioning if no two objects that compare equal fall into different partitions.

23.2.8.2 Exception safety guarantees [unord.req.except]

1

#

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 orPred object (if any).

2

#

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

3

#

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

4

#

For unordered associative containers, if an exception is thrown from within a rehash() function other than by the container's hash function or comparison function, the rehash() function has no effect.