1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <unordered_map>
10
11// template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12// class Alloc = allocator<pair<const Key, T>>>
13// class unordered_multimap
14
15// size_type erase(const key_type& k);
16
17#include <unordered_map>
18#include <string>
19#include <set>
20#include <cassert>
21#include <cstddef>
22
23#include "test_macros.h"
24#include "../../../check_consecutive.h"
25#include "min_allocator.h"
26
27#if TEST_STD_VER >= 11
28template <typename Unordered>
29bool only_deletions ( const Unordered &whole, const Unordered &part ) {
30 typename Unordered::const_iterator w = whole.begin();
31 typename Unordered::const_iterator p = part.begin();
32
33 while ( w != whole.end () && p != part.end()) {
34 if ( *w == *p )
35 p++;
36 w++;
37 }
38
39 return p == part.end();
40}
41#endif
42
43int main(int, char**)
44{
45 {
46 typedef std::unordered_multimap<int, std::string> C;
47 typedef std::pair<int, std::string> P;
48 P a[] =
49 {
50 P(1, "one"),
51 P(2, "two"),
52 P(3, "three"),
53 P(4, "four"),
54 P(1, "four"),
55 P(2, "four"),
56 };
57 C c(a, a + sizeof(a)/sizeof(a[0]));
58 assert(c.erase(5) == 0);
59 assert(c.size() == 6);
60 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
61 Eq eq = c.equal_range(x: 1);
62 assert(std::distance(eq.first, eq.second) == 2);
63 std::multiset<std::string> s;
64 s.insert(x: "one");
65 s.insert(x: "four");
66 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
67 eq = c.equal_range(x: 2);
68 assert(std::distance(eq.first, eq.second) == 2);
69 s.insert(x: "two");
70 s.insert(x: "four");
71 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 2), end: c.end(), key: 2, values&: s);
72 eq = c.equal_range(x: 3);
73 assert(std::distance(eq.first, eq.second) == 1);
74 C::const_iterator k = eq.first;
75 assert(k->first == 3);
76 assert(k->second == "three");
77 eq = c.equal_range(x: 4);
78 assert(std::distance(eq.first, eq.second) == 1);
79 k = eq.first;
80 assert(k->first == 4);
81 assert(k->second == "four");
82 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
83 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
84
85 assert(c.erase(2) == 2);
86 assert(c.size() == 4);
87 eq = c.equal_range(x: 1);
88 assert(std::distance(eq.first, eq.second) == 2);
89 s.insert(x: "one");
90 s.insert(x: "four");
91 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
92 eq = c.equal_range(x: 3);
93 assert(std::distance(eq.first, eq.second) == 1);
94 k = eq.first;
95 assert(k->first == 3);
96 assert(k->second == "three");
97 eq = c.equal_range(x: 4);
98 assert(std::distance(eq.first, eq.second) == 1);
99 k = eq.first;
100 assert(k->first == 4);
101 assert(k->second == "four");
102 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
103 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
104
105 assert(c.erase(2) == 0);
106 assert(c.size() == 4);
107 eq = c.equal_range(x: 1);
108 assert(std::distance(eq.first, eq.second) == 2);
109 s.insert(x: "one");
110 s.insert(x: "four");
111 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
112 eq = c.equal_range(x: 3);
113 assert(std::distance(eq.first, eq.second) == 1);
114 k = eq.first;
115 assert(k->first == 3);
116 assert(k->second == "three");
117 eq = c.equal_range(x: 4);
118 assert(std::distance(eq.first, eq.second) == 1);
119 k = eq.first;
120 assert(k->first == 4);
121 assert(k->second == "four");
122 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
123 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
124
125 assert(c.erase(4) == 1);
126 assert(c.size() == 3);
127 eq = c.equal_range(x: 1);
128 assert(std::distance(eq.first, eq.second) == 2);
129 s.insert(x: "one");
130 s.insert(x: "four");
131 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
132 eq = c.equal_range(x: 3);
133 assert(std::distance(eq.first, eq.second) == 1);
134 k = eq.first;
135 assert(k->first == 3);
136 assert(k->second == "three");
137 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
138 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
139
140 assert(c.erase(4) == 0);
141 assert(c.size() == 3);
142 eq = c.equal_range(x: 1);
143 assert(std::distance(eq.first, eq.second) == 2);
144 s.insert(x: "one");
145 s.insert(x: "four");
146 CheckConsecutiveKeys<C::const_iterator>(pos: c.find(x: 1), end: c.end(), key: 1, values&: s);
147 eq = c.equal_range(x: 3);
148 assert(std::distance(eq.first, eq.second) == 1);
149 k = eq.first;
150 assert(k->first == 3);
151 assert(k->second == "three");
152 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
153 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
154
155 assert(c.erase(1) == 2);
156 assert(c.size() == 1);
157 eq = c.equal_range(x: 3);
158 assert(std::distance(eq.first, eq.second) == 1);
159 k = eq.first;
160 assert(k->first == 3);
161 assert(k->second == "three");
162 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
163 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
164
165 assert(c.erase(1) == 0);
166 assert(c.size() == 1);
167 eq = c.equal_range(x: 3);
168 assert(std::distance(eq.first, eq.second) == 1);
169 k = eq.first;
170 assert(k->first == 3);
171 assert(k->second == "three");
172 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
173 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
174
175 assert(c.erase(3) == 1);
176 assert(c.size() == 0);
177 eq = c.equal_range(x: 3);
178 assert(std::distance(eq.first, eq.second) == 0);
179 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
180 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
181
182 assert(c.erase(3) == 0);
183 assert(c.size() == 0);
184 eq = c.equal_range(x: 3);
185 assert(std::distance(eq.first, eq.second) == 0);
186 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
187 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
188 }
189#if TEST_STD_VER >= 11
190 {
191 typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
192 min_allocator<std::pair<const int, std::string>>> C;
193 typedef std::pair<int, std::string> P;
194 P a[] =
195 {
196 P(1, "one"),
197 P(2, "two"),
198 P(3, "three"),
199 P(4, "four"),
200 P(1, "four"),
201 P(2, "four"),
202 };
203 C c(a, a + sizeof(a)/sizeof(a[0]));
204 assert(c.erase(5) == 0);
205 assert(c.size() == 6);
206 typedef std::pair<C::const_iterator, C::const_iterator> Eq;
207 Eq eq = c.equal_range(1);
208 assert(std::distance(eq.first, eq.second) == 2);
209 std::multiset<std::string> s;
210 s.insert("one");
211 s.insert("four");
212 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
213 eq = c.equal_range(2);
214 assert(std::distance(eq.first, eq.second) == 2);
215 s.insert("two");
216 s.insert("four");
217 CheckConsecutiveKeys<C::const_iterator>(c.find(2), c.end(), 2, s);
218 eq = c.equal_range(3);
219 assert(std::distance(eq.first, eq.second) == 1);
220 C::const_iterator k = eq.first;
221 assert(k->first == 3);
222 assert(k->second == "three");
223 eq = c.equal_range(4);
224 assert(std::distance(eq.first, eq.second) == 1);
225 k = eq.first;
226 assert(k->first == 4);
227 assert(k->second == "four");
228 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
229 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
230
231 assert(c.erase(2) == 2);
232 assert(c.size() == 4);
233 eq = c.equal_range(1);
234 assert(std::distance(eq.first, eq.second) == 2);
235 s.insert("one");
236 s.insert("four");
237 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
238 eq = c.equal_range(3);
239 assert(std::distance(eq.first, eq.second) == 1);
240 k = eq.first;
241 assert(k->first == 3);
242 assert(k->second == "three");
243 eq = c.equal_range(4);
244 assert(std::distance(eq.first, eq.second) == 1);
245 k = eq.first;
246 assert(k->first == 4);
247 assert(k->second == "four");
248 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
249 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
250
251 assert(c.erase(2) == 0);
252 assert(c.size() == 4);
253 eq = c.equal_range(1);
254 assert(std::distance(eq.first, eq.second) == 2);
255 s.insert("one");
256 s.insert("four");
257 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
258 eq = c.equal_range(3);
259 assert(std::distance(eq.first, eq.second) == 1);
260 k = eq.first;
261 assert(k->first == 3);
262 assert(k->second == "three");
263 eq = c.equal_range(4);
264 assert(std::distance(eq.first, eq.second) == 1);
265 k = eq.first;
266 assert(k->first == 4);
267 assert(k->second == "four");
268 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
269 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
270
271 assert(c.erase(4) == 1);
272 assert(c.size() == 3);
273 eq = c.equal_range(1);
274 assert(std::distance(eq.first, eq.second) == 2);
275 s.insert("one");
276 s.insert("four");
277 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
278 eq = c.equal_range(3);
279 assert(std::distance(eq.first, eq.second) == 1);
280 k = eq.first;
281 assert(k->first == 3);
282 assert(k->second == "three");
283 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
284 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
285
286 assert(c.erase(4) == 0);
287 assert(c.size() == 3);
288 eq = c.equal_range(1);
289 assert(std::distance(eq.first, eq.second) == 2);
290 s.insert("one");
291 s.insert("four");
292 CheckConsecutiveKeys<C::const_iterator>(c.find(1), c.end(), 1, s);
293 eq = c.equal_range(3);
294 assert(std::distance(eq.first, eq.second) == 1);
295 k = eq.first;
296 assert(k->first == 3);
297 assert(k->second == "three");
298 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
299 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
300
301 assert(c.erase(1) == 2);
302 assert(c.size() == 1);
303 eq = c.equal_range(3);
304 assert(std::distance(eq.first, eq.second) == 1);
305 k = eq.first;
306 assert(k->first == 3);
307 assert(k->second == "three");
308 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
309 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
310
311 assert(c.erase(1) == 0);
312 assert(c.size() == 1);
313 eq = c.equal_range(3);
314 assert(std::distance(eq.first, eq.second) == 1);
315 k = eq.first;
316 assert(k->first == 3);
317 assert(k->second == "three");
318 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
319 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
320
321 assert(c.erase(3) == 1);
322 assert(c.size() == 0);
323 eq = c.equal_range(3);
324 assert(std::distance(eq.first, eq.second) == 0);
325 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
326 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
327
328 assert(c.erase(3) == 0);
329 assert(c.size() == 0);
330 eq = c.equal_range(3);
331 assert(std::distance(eq.first, eq.second) == 0);
332 assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
333 assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
334 }
335 {
336 typedef std::unordered_multimap<int, int> C;
337 C m, m2;
338 for ( int i = 0; i < 10; ++i ) {
339 for (int j = 0; j < 2; ++j ) {
340 m.insert (std::make_pair(i,j));
341 m2.insert(std::make_pair(i,j));
342 }
343 }
344
345 C::iterator i = m2.begin();
346 int ctr = 0;
347 while (i != m2.end()) {
348 if (ctr++ % 2 == 0)
349 m2.erase(i++);
350 else
351 ++i;
352 }
353
354 assert (only_deletions (m, m2));
355 }
356#endif
357
358 return 0;
359}
360

source code of libcxx/test/std/containers/unord/unord.multimap/unord.multimap.modifiers/erase_key.pass.cpp