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 map
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::map<int, double> M;
33 typedef std::pair<int, double> P;
34 typedef M::iterator I;
35 P ar[] =
36 {
37 P(1, 1.5),
38 P(2, 2.5),
39 P(3, 3.5),
40 P(4, 4.5),
41 P(5, 5.5),
42 P(6, 6.5),
43 P(7, 7.5),
44 P(8, 8.5),
45 };
46 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
47 assert(m.size() == 8);
48 I i = m.erase(position: std::next(x: m.cbegin(), n: 3));
49 assert(m.size() == 7);
50 assert(i == std::next(m.begin(), 3));
51 assert(m.begin()->first == 1);
52 assert(m.begin()->second == 1.5);
53 assert(std::next(m.begin())->first == 2);
54 assert(std::next(m.begin())->second == 2.5);
55 assert(std::next(m.begin(), 2)->first == 3);
56 assert(std::next(m.begin(), 2)->second == 3.5);
57 assert(std::next(m.begin(), 3)->first == 5);
58 assert(std::next(m.begin(), 3)->second == 5.5);
59 assert(std::next(m.begin(), 4)->first == 6);
60 assert(std::next(m.begin(), 4)->second == 6.5);
61 assert(std::next(m.begin(), 5)->first == 7);
62 assert(std::next(m.begin(), 5)->second == 7.5);
63 assert(std::next(m.begin(), 6)->first == 8);
64 assert(std::next(m.begin(), 6)->second == 8.5);
65
66 i = m.erase(position: std::next(x: m.cbegin(), n: 0));
67 assert(m.size() == 6);
68 assert(i == m.begin());
69 assert(m.begin()->first == 2);
70 assert(m.begin()->second == 2.5);
71 assert(std::next(m.begin())->first == 3);
72 assert(std::next(m.begin())->second == 3.5);
73 assert(std::next(m.begin(), 2)->first == 5);
74 assert(std::next(m.begin(), 2)->second == 5.5);
75 assert(std::next(m.begin(), 3)->first == 6);
76 assert(std::next(m.begin(), 3)->second == 6.5);
77 assert(std::next(m.begin(), 4)->first == 7);
78 assert(std::next(m.begin(), 4)->second == 7.5);
79 assert(std::next(m.begin(), 5)->first == 8);
80 assert(std::next(m.begin(), 5)->second == 8.5);
81
82 i = m.erase(position: std::next(x: m.cbegin(), n: 5));
83 assert(m.size() == 5);
84 assert(i == m.end());
85 assert(m.begin()->first == 2);
86 assert(m.begin()->second == 2.5);
87 assert(std::next(m.begin())->first == 3);
88 assert(std::next(m.begin())->second == 3.5);
89 assert(std::next(m.begin(), 2)->first == 5);
90 assert(std::next(m.begin(), 2)->second == 5.5);
91 assert(std::next(m.begin(), 3)->first == 6);
92 assert(std::next(m.begin(), 3)->second == 6.5);
93 assert(std::next(m.begin(), 4)->first == 7);
94 assert(std::next(m.begin(), 4)->second == 7.5);
95
96 i = m.erase(position: std::next(x: m.cbegin(), n: 1));
97 assert(m.size() == 4);
98 assert(i == std::next(m.begin()));
99 assert(m.begin()->first == 2);
100 assert(m.begin()->second == 2.5);
101 assert(std::next(m.begin())->first == 5);
102 assert(std::next(m.begin())->second == 5.5);
103 assert(std::next(m.begin(), 2)->first == 6);
104 assert(std::next(m.begin(), 2)->second == 6.5);
105 assert(std::next(m.begin(), 3)->first == 7);
106 assert(std::next(m.begin(), 3)->second == 7.5);
107
108 i = m.erase(position: std::next(x: m.cbegin(), n: 2));
109 assert(m.size() == 3);
110 assert(i == std::next(m.begin(), 2));
111 assert(m.begin()->first == 2);
112 assert(m.begin()->second == 2.5);
113 assert(std::next(m.begin())->first == 5);
114 assert(std::next(m.begin())->second == 5.5);
115 assert(std::next(m.begin(), 2)->first == 7);
116 assert(std::next(m.begin(), 2)->second == 7.5);
117
118 i = m.erase(position: std::next(x: m.cbegin(), n: 2));
119 assert(m.size() == 2);
120 assert(i == std::next(m.begin(), 2));
121 assert(m.begin()->first == 2);
122 assert(m.begin()->second == 2.5);
123 assert(std::next(m.begin())->first == 5);
124 assert(std::next(m.begin())->second == 5.5);
125
126 i = m.erase(position: std::next(x: m.cbegin(), n: 0));
127 assert(m.size() == 1);
128 assert(i == std::next(m.begin(), 0));
129 assert(m.begin()->first == 5);
130 assert(m.begin()->second == 5.5);
131
132 i = m.erase(position: m.cbegin());
133 assert(m.size() == 0);
134 assert(i == m.begin());
135 assert(i == m.end());
136 }
137#if TEST_STD_VER >= 11
138 {
139 typedef std::map<int, double, std::less<int>, min_allocator<std::pair<const int, double>>> M;
140 typedef std::pair<int, double> P;
141 typedef M::iterator I;
142 P ar[] =
143 {
144 P(1, 1.5),
145 P(2, 2.5),
146 P(3, 3.5),
147 P(4, 4.5),
148 P(5, 5.5),
149 P(6, 6.5),
150 P(7, 7.5),
151 P(8, 8.5),
152 };
153 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
154 assert(m.size() == 8);
155 I i = m.erase(std::next(m.cbegin(), 3));
156 assert(m.size() == 7);
157 assert(i == std::next(m.begin(), 3));
158 assert(m.begin()->first == 1);
159 assert(m.begin()->second == 1.5);
160 assert(std::next(m.begin())->first == 2);
161 assert(std::next(m.begin())->second == 2.5);
162 assert(std::next(m.begin(), 2)->first == 3);
163 assert(std::next(m.begin(), 2)->second == 3.5);
164 assert(std::next(m.begin(), 3)->first == 5);
165 assert(std::next(m.begin(), 3)->second == 5.5);
166 assert(std::next(m.begin(), 4)->first == 6);
167 assert(std::next(m.begin(), 4)->second == 6.5);
168 assert(std::next(m.begin(), 5)->first == 7);
169 assert(std::next(m.begin(), 5)->second == 7.5);
170 assert(std::next(m.begin(), 6)->first == 8);
171 assert(std::next(m.begin(), 6)->second == 8.5);
172
173 i = m.erase(std::next(m.cbegin(), 0));
174 assert(m.size() == 6);
175 assert(i == m.begin());
176 assert(m.begin()->first == 2);
177 assert(m.begin()->second == 2.5);
178 assert(std::next(m.begin())->first == 3);
179 assert(std::next(m.begin())->second == 3.5);
180 assert(std::next(m.begin(), 2)->first == 5);
181 assert(std::next(m.begin(), 2)->second == 5.5);
182 assert(std::next(m.begin(), 3)->first == 6);
183 assert(std::next(m.begin(), 3)->second == 6.5);
184 assert(std::next(m.begin(), 4)->first == 7);
185 assert(std::next(m.begin(), 4)->second == 7.5);
186 assert(std::next(m.begin(), 5)->first == 8);
187 assert(std::next(m.begin(), 5)->second == 8.5);
188
189 i = m.erase(std::next(m.cbegin(), 5));
190 assert(m.size() == 5);
191 assert(i == m.end());
192 assert(m.begin()->first == 2);
193 assert(m.begin()->second == 2.5);
194 assert(std::next(m.begin())->first == 3);
195 assert(std::next(m.begin())->second == 3.5);
196 assert(std::next(m.begin(), 2)->first == 5);
197 assert(std::next(m.begin(), 2)->second == 5.5);
198 assert(std::next(m.begin(), 3)->first == 6);
199 assert(std::next(m.begin(), 3)->second == 6.5);
200 assert(std::next(m.begin(), 4)->first == 7);
201 assert(std::next(m.begin(), 4)->second == 7.5);
202
203 i = m.erase(std::next(m.cbegin(), 1));
204 assert(m.size() == 4);
205 assert(i == std::next(m.begin()));
206 assert(m.begin()->first == 2);
207 assert(m.begin()->second == 2.5);
208 assert(std::next(m.begin())->first == 5);
209 assert(std::next(m.begin())->second == 5.5);
210 assert(std::next(m.begin(), 2)->first == 6);
211 assert(std::next(m.begin(), 2)->second == 6.5);
212 assert(std::next(m.begin(), 3)->first == 7);
213 assert(std::next(m.begin(), 3)->second == 7.5);
214
215 i = m.erase(std::next(m.cbegin(), 2));
216 assert(m.size() == 3);
217 assert(i == std::next(m.begin(), 2));
218 assert(m.begin()->first == 2);
219 assert(m.begin()->second == 2.5);
220 assert(std::next(m.begin())->first == 5);
221 assert(std::next(m.begin())->second == 5.5);
222 assert(std::next(m.begin(), 2)->first == 7);
223 assert(std::next(m.begin(), 2)->second == 7.5);
224
225 i = m.erase(std::next(m.cbegin(), 2));
226 assert(m.size() == 2);
227 assert(i == std::next(m.begin(), 2));
228 assert(m.begin()->first == 2);
229 assert(m.begin()->second == 2.5);
230 assert(std::next(m.begin())->first == 5);
231 assert(std::next(m.begin())->second == 5.5);
232
233 i = m.erase(std::next(m.cbegin(), 0));
234 assert(m.size() == 1);
235 assert(i == std::next(m.begin(), 0));
236 assert(m.begin()->first == 5);
237 assert(m.begin()->second == 5.5);
238
239 i = m.erase(m.cbegin());
240 assert(m.size() == 0);
241 assert(i == m.begin());
242 assert(i == m.end());
243 }
244#endif
245#if TEST_STD_VER >= 14
246 {
247 // This is LWG #2059
248 typedef TemplateConstructor T;
249 typedef std::map<T, int> C;
250 typedef C::iterator I;
251
252 C c;
253 T a{0};
254 I it = c.find(a);
255 if (it != c.end())
256 c.erase(it);
257 }
258#endif
259
260 return 0;
261}
262

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