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// <map>
10
11// class multimap
12
13// iterator erase(const_iterator position);
14
15#include <map>
16#include <cassert>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20
21struct TemplateConstructor
22{
23 template<typename T>
24 TemplateConstructor (const T&) {}
25};
26
27bool operator<(const TemplateConstructor&, const TemplateConstructor&) { return false; }
28
29int main(int, char**)
30{
31 {
32 typedef std::multimap<int, double> M;
33 typedef std::pair<int, double> P;
34 typedef M::iterator I;
35 P ar[] =
36 {
37 P(1, 1),
38 P(1, 1.5),
39 P(1, 2),
40 P(2, 1),
41 P(2, 1.5),
42 P(2, 2),
43 P(3, 1),
44 P(3, 1.5),
45 P(3, 2),
46 };
47 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
48 assert(m.size() == 9);
49 I i = m.erase(position: std::next(x: m.cbegin(), n: 3));
50 assert(m.size() == 8);
51 assert(i == std::next(m.begin(), 3));
52 assert(m.begin()->first == 1);
53 assert(m.begin()->second == 1);
54 assert(std::next(m.begin())->first == 1);
55 assert(std::next(m.begin())->second == 1.5);
56 assert(std::next(m.begin(), 2)->first == 1);
57 assert(std::next(m.begin(), 2)->second == 2);
58 assert(std::next(m.begin(), 3)->first == 2);
59 assert(std::next(m.begin(), 3)->second == 1.5);
60 assert(std::next(m.begin(), 4)->first == 2);
61 assert(std::next(m.begin(), 4)->second == 2);
62 assert(std::next(m.begin(), 5)->first == 3);
63 assert(std::next(m.begin(), 5)->second == 1);
64 assert(std::next(m.begin(), 6)->first == 3);
65 assert(std::next(m.begin(), 6)->second == 1.5);
66 assert(std::next(m.begin(), 7)->first == 3);
67 assert(std::next(m.begin(), 7)->second == 2);
68
69 i = m.erase(position: std::next(x: m.cbegin(), n: 0));
70 assert(m.size() == 7);
71 assert(i == m.begin());
72 assert(std::next(m.begin(), 0)->first == 1);
73 assert(std::next(m.begin(), 0)->second == 1.5);
74 assert(std::next(m.begin(), 1)->first == 1);
75 assert(std::next(m.begin(), 1)->second == 2);
76 assert(std::next(m.begin(), 2)->first == 2);
77 assert(std::next(m.begin(), 2)->second == 1.5);
78 assert(std::next(m.begin(), 3)->first == 2);
79 assert(std::next(m.begin(), 3)->second == 2);
80 assert(std::next(m.begin(), 4)->first == 3);
81 assert(std::next(m.begin(), 4)->second == 1);
82 assert(std::next(m.begin(), 5)->first == 3);
83 assert(std::next(m.begin(), 5)->second == 1.5);
84 assert(std::next(m.begin(), 6)->first == 3);
85 assert(std::next(m.begin(), 6)->second == 2);
86
87 i = m.erase(position: std::next(x: m.cbegin(), n: 5));
88 assert(m.size() == 6);
89 assert(i == std::prev(m.end()));
90 assert(std::next(m.begin(), 0)->first == 1);
91 assert(std::next(m.begin(), 0)->second == 1.5);
92 assert(std::next(m.begin(), 1)->first == 1);
93 assert(std::next(m.begin(), 1)->second == 2);
94 assert(std::next(m.begin(), 2)->first == 2);
95 assert(std::next(m.begin(), 2)->second == 1.5);
96 assert(std::next(m.begin(), 3)->first == 2);
97 assert(std::next(m.begin(), 3)->second == 2);
98 assert(std::next(m.begin(), 4)->first == 3);
99 assert(std::next(m.begin(), 4)->second == 1);
100 assert(std::next(m.begin(), 5)->first == 3);
101 assert(std::next(m.begin(), 5)->second == 2);
102
103 i = m.erase(position: std::next(x: m.cbegin(), n: 1));
104 assert(m.size() == 5);
105 assert(i == std::next(m.begin()));
106 assert(std::next(m.begin(), 0)->first == 1);
107 assert(std::next(m.begin(), 0)->second == 1.5);
108 assert(std::next(m.begin(), 1)->first == 2);
109 assert(std::next(m.begin(), 1)->second == 1.5);
110 assert(std::next(m.begin(), 2)->first == 2);
111 assert(std::next(m.begin(), 2)->second == 2);
112 assert(std::next(m.begin(), 3)->first == 3);
113 assert(std::next(m.begin(), 3)->second == 1);
114 assert(std::next(m.begin(), 4)->first == 3);
115 assert(std::next(m.begin(), 4)->second == 2);
116
117 i = m.erase(position: std::next(x: m.cbegin(), n: 2));
118 assert(m.size() == 4);
119 assert(i == std::next(m.begin(), 2));
120 assert(std::next(m.begin(), 0)->first == 1);
121 assert(std::next(m.begin(), 0)->second == 1.5);
122 assert(std::next(m.begin(), 1)->first == 2);
123 assert(std::next(m.begin(), 1)->second == 1.5);
124 assert(std::next(m.begin(), 2)->first == 3);
125 assert(std::next(m.begin(), 2)->second == 1);
126 assert(std::next(m.begin(), 3)->first == 3);
127 assert(std::next(m.begin(), 3)->second == 2);
128
129 i = m.erase(position: std::next(x: m.cbegin(), n: 2));
130 assert(m.size() == 3);
131 assert(i == std::next(m.begin(), 2));
132 assert(std::next(m.begin(), 0)->first == 1);
133 assert(std::next(m.begin(), 0)->second == 1.5);
134 assert(std::next(m.begin(), 1)->first == 2);
135 assert(std::next(m.begin(), 1)->second == 1.5);
136 assert(std::next(m.begin(), 2)->first == 3);
137 assert(std::next(m.begin(), 2)->second == 2);
138
139 i = m.erase(position: std::next(x: m.cbegin(), n: 0));
140 assert(m.size() == 2);
141 assert(i == std::next(m.begin(), 0));
142 assert(std::next(m.begin(), 0)->first == 2);
143 assert(std::next(m.begin(), 0)->second == 1.5);
144 assert(std::next(m.begin(), 1)->first == 3);
145 assert(std::next(m.begin(), 1)->second == 2);
146
147 i = m.erase(position: std::next(x: m.cbegin(), n: 1));
148 assert(m.size() == 1);
149 assert(i == m.end());
150 assert(std::next(m.begin(), 0)->first == 2);
151 assert(std::next(m.begin(), 0)->second == 1.5);
152
153 i = m.erase(position: m.cbegin());
154 assert(m.size() == 0);
155 assert(i == m.begin());
156 assert(i == m.end());
157 }
158#if TEST_STD_VER >= 11
159 {
160 typedef std::multimap<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
161 typedef std::pair<int, double> P;
162 typedef M::iterator I;
163 P ar[] =
164 {
165 P(1, 1),
166 P(1, 1.5),
167 P(1, 2),
168 P(2, 1),
169 P(2, 1.5),
170 P(2, 2),
171 P(3, 1),
172 P(3, 1.5),
173 P(3, 2),
174 };
175 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
176 assert(m.size() == 9);
177 I i = m.erase(std::next(m.cbegin(), 3));
178 assert(m.size() == 8);
179 assert(i == std::next(m.begin(), 3));
180 assert(m.begin()->first == 1);
181 assert(m.begin()->second == 1);
182 assert(std::next(m.begin())->first == 1);
183 assert(std::next(m.begin())->second == 1.5);
184 assert(std::next(m.begin(), 2)->first == 1);
185 assert(std::next(m.begin(), 2)->second == 2);
186 assert(std::next(m.begin(), 3)->first == 2);
187 assert(std::next(m.begin(), 3)->second == 1.5);
188 assert(std::next(m.begin(), 4)->first == 2);
189 assert(std::next(m.begin(), 4)->second == 2);
190 assert(std::next(m.begin(), 5)->first == 3);
191 assert(std::next(m.begin(), 5)->second == 1);
192 assert(std::next(m.begin(), 6)->first == 3);
193 assert(std::next(m.begin(), 6)->second == 1.5);
194 assert(std::next(m.begin(), 7)->first == 3);
195 assert(std::next(m.begin(), 7)->second == 2);
196
197 i = m.erase(std::next(m.cbegin(), 0));
198 assert(m.size() == 7);
199 assert(i == m.begin());
200 assert(std::next(m.begin(), 0)->first == 1);
201 assert(std::next(m.begin(), 0)->second == 1.5);
202 assert(std::next(m.begin(), 1)->first == 1);
203 assert(std::next(m.begin(), 1)->second == 2);
204 assert(std::next(m.begin(), 2)->first == 2);
205 assert(std::next(m.begin(), 2)->second == 1.5);
206 assert(std::next(m.begin(), 3)->first == 2);
207 assert(std::next(m.begin(), 3)->second == 2);
208 assert(std::next(m.begin(), 4)->first == 3);
209 assert(std::next(m.begin(), 4)->second == 1);
210 assert(std::next(m.begin(), 5)->first == 3);
211 assert(std::next(m.begin(), 5)->second == 1.5);
212 assert(std::next(m.begin(), 6)->first == 3);
213 assert(std::next(m.begin(), 6)->second == 2);
214
215 i = m.erase(std::next(m.cbegin(), 5));
216 assert(m.size() == 6);
217 assert(i == std::prev(m.end()));
218 assert(std::next(m.begin(), 0)->first == 1);
219 assert(std::next(m.begin(), 0)->second == 1.5);
220 assert(std::next(m.begin(), 1)->first == 1);
221 assert(std::next(m.begin(), 1)->second == 2);
222 assert(std::next(m.begin(), 2)->first == 2);
223 assert(std::next(m.begin(), 2)->second == 1.5);
224 assert(std::next(m.begin(), 3)->first == 2);
225 assert(std::next(m.begin(), 3)->second == 2);
226 assert(std::next(m.begin(), 4)->first == 3);
227 assert(std::next(m.begin(), 4)->second == 1);
228 assert(std::next(m.begin(), 5)->first == 3);
229 assert(std::next(m.begin(), 5)->second == 2);
230
231 i = m.erase(std::next(m.cbegin(), 1));
232 assert(m.size() == 5);
233 assert(i == std::next(m.begin()));
234 assert(std::next(m.begin(), 0)->first == 1);
235 assert(std::next(m.begin(), 0)->second == 1.5);
236 assert(std::next(m.begin(), 1)->first == 2);
237 assert(std::next(m.begin(), 1)->second == 1.5);
238 assert(std::next(m.begin(), 2)->first == 2);
239 assert(std::next(m.begin(), 2)->second == 2);
240 assert(std::next(m.begin(), 3)->first == 3);
241 assert(std::next(m.begin(), 3)->second == 1);
242 assert(std::next(m.begin(), 4)->first == 3);
243 assert(std::next(m.begin(), 4)->second == 2);
244
245 i = m.erase(std::next(m.cbegin(), 2));
246 assert(m.size() == 4);
247 assert(i == std::next(m.begin(), 2));
248 assert(std::next(m.begin(), 0)->first == 1);
249 assert(std::next(m.begin(), 0)->second == 1.5);
250 assert(std::next(m.begin(), 1)->first == 2);
251 assert(std::next(m.begin(), 1)->second == 1.5);
252 assert(std::next(m.begin(), 2)->first == 3);
253 assert(std::next(m.begin(), 2)->second == 1);
254 assert(std::next(m.begin(), 3)->first == 3);
255 assert(std::next(m.begin(), 3)->second == 2);
256
257 i = m.erase(std::next(m.cbegin(), 2));
258 assert(m.size() == 3);
259 assert(i == std::next(m.begin(), 2));
260 assert(std::next(m.begin(), 0)->first == 1);
261 assert(std::next(m.begin(), 0)->second == 1.5);
262 assert(std::next(m.begin(), 1)->first == 2);
263 assert(std::next(m.begin(), 1)->second == 1.5);
264 assert(std::next(m.begin(), 2)->first == 3);
265 assert(std::next(m.begin(), 2)->second == 2);
266
267 i = m.erase(std::next(m.cbegin(), 0));
268 assert(m.size() == 2);
269 assert(i == std::next(m.begin(), 0));
270 assert(std::next(m.begin(), 0)->first == 2);
271 assert(std::next(m.begin(), 0)->second == 1.5);
272 assert(std::next(m.begin(), 1)->first == 3);
273 assert(std::next(m.begin(), 1)->second == 2);
274
275 i = m.erase(std::next(m.cbegin(), 1));
276 assert(m.size() == 1);
277 assert(i == m.end());
278 assert(std::next(m.begin(), 0)->first == 2);
279 assert(std::next(m.begin(), 0)->second == 1.5);
280
281 i = m.erase(m.cbegin());
282 assert(m.size() == 0);
283 assert(i == m.begin());
284 assert(i == m.end());
285 }
286#endif
287#if TEST_STD_VER >= 14
288 {
289 // This is LWG #2059
290 typedef TemplateConstructor T;
291 typedef std::multimap<T, int> C;
292 typedef C::iterator I;
293
294 C c;
295 T a{0};
296 I it = c.find(a);
297 if (it != c.end())
298 c.erase(it);
299 }
300#endif
301
302 return 0;
303}
304

source code of libcxx/test/std/containers/associative/multimap/multimap.modifiers/erase_iter.pass.cpp