1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
13 // iterator erase(const_iterator position);
18 #include "test_macros.h"
19 #include "min_allocator.h"
21 struct TemplateConstructor
24 TemplateConstructor (const T
&) {}
27 bool operator<(const TemplateConstructor
&, const TemplateConstructor
&) { return false; }
32 typedef std::map
<int, double> M
;
33 typedef std::pair
<int, double> P
;
34 typedef M::iterator I
;
46 M
m(ar
, ar
+ sizeof(ar
)/sizeof(ar
[0]));
47 assert(m
.size() == 8);
48 I i
= m
.erase(std::next(m
.cbegin(), 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);
66 i
= m
.erase(std::next(m
.cbegin(), 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);
82 i
= m
.erase(std::next(m
.cbegin(), 5));
83 assert(m
.size() == 5);
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);
96 i
= m
.erase(std::next(m
.cbegin(), 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);
108 i
= m
.erase(std::next(m
.cbegin(), 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);
118 i
= m
.erase(std::next(m
.cbegin(), 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);
126 i
= m
.erase(std::next(m
.cbegin(), 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);
132 i
= m
.erase(m
.cbegin());
133 assert(m
.size() == 0);
134 assert(i
== m
.begin());
135 assert(i
== m
.end());
137 #if TEST_STD_VER >= 11
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
;
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);
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);
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);
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);
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);
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);
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);
239 i
= m
.erase(m
.cbegin());
240 assert(m
.size() == 0);
241 assert(i
== m
.begin());
242 assert(i
== m
.end());
245 #if TEST_STD_VER >= 14
248 typedef TemplateConstructor T
;
249 typedef std::map
<T
, int> C
;
250 typedef C::iterator I
;