1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <map>
11
12 // class map
13
14 // iterator erase(const_iterator position);
15
16 #include <map>
17 #include <cassert>
18
19 #include "min_allocator.h"
20
21 struct TemplateConstructor
22 {
23 template<typename T>
TemplateConstructorTemplateConstructor24 TemplateConstructor (const T&) {}
25 };
26
operator <(const TemplateConstructor &,const TemplateConstructor &)27 bool operator<(const TemplateConstructor&, const TemplateConstructor&) { return false; }
28
main()29 int main()
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(next(m.cbegin(), 3));
49 assert(m.size() == 7);
50 assert(i == next(m.begin(), 3));
51 assert(m.begin()->first == 1);
52 assert(m.begin()->second == 1.5);
53 assert(next(m.begin())->first == 2);
54 assert(next(m.begin())->second == 2.5);
55 assert(next(m.begin(), 2)->first == 3);
56 assert(next(m.begin(), 2)->second == 3.5);
57 assert(next(m.begin(), 3)->first == 5);
58 assert(next(m.begin(), 3)->second == 5.5);
59 assert(next(m.begin(), 4)->first == 6);
60 assert(next(m.begin(), 4)->second == 6.5);
61 assert(next(m.begin(), 5)->first == 7);
62 assert(next(m.begin(), 5)->second == 7.5);
63 assert(next(m.begin(), 6)->first == 8);
64 assert(next(m.begin(), 6)->second == 8.5);
65
66 i = m.erase(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(next(m.begin())->first == 3);
72 assert(next(m.begin())->second == 3.5);
73 assert(next(m.begin(), 2)->first == 5);
74 assert(next(m.begin(), 2)->second == 5.5);
75 assert(next(m.begin(), 3)->first == 6);
76 assert(next(m.begin(), 3)->second == 6.5);
77 assert(next(m.begin(), 4)->first == 7);
78 assert(next(m.begin(), 4)->second == 7.5);
79 assert(next(m.begin(), 5)->first == 8);
80 assert(next(m.begin(), 5)->second == 8.5);
81
82 i = m.erase(next(m.cbegin(), 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(next(m.begin())->first == 3);
88 assert(next(m.begin())->second == 3.5);
89 assert(next(m.begin(), 2)->first == 5);
90 assert(next(m.begin(), 2)->second == 5.5);
91 assert(next(m.begin(), 3)->first == 6);
92 assert(next(m.begin(), 3)->second == 6.5);
93 assert(next(m.begin(), 4)->first == 7);
94 assert(next(m.begin(), 4)->second == 7.5);
95
96 i = m.erase(next(m.cbegin(), 1));
97 assert(m.size() == 4);
98 assert(i == next(m.begin()));
99 assert(m.begin()->first == 2);
100 assert(m.begin()->second == 2.5);
101 assert(next(m.begin())->first == 5);
102 assert(next(m.begin())->second == 5.5);
103 assert(next(m.begin(), 2)->first == 6);
104 assert(next(m.begin(), 2)->second == 6.5);
105 assert(next(m.begin(), 3)->first == 7);
106 assert(next(m.begin(), 3)->second == 7.5);
107
108 i = m.erase(next(m.cbegin(), 2));
109 assert(m.size() == 3);
110 assert(i == next(m.begin(), 2));
111 assert(m.begin()->first == 2);
112 assert(m.begin()->second == 2.5);
113 assert(next(m.begin())->first == 5);
114 assert(next(m.begin())->second == 5.5);
115 assert(next(m.begin(), 2)->first == 7);
116 assert(next(m.begin(), 2)->second == 7.5);
117
118 i = m.erase(next(m.cbegin(), 2));
119 assert(m.size() == 2);
120 assert(i == next(m.begin(), 2));
121 assert(m.begin()->first == 2);
122 assert(m.begin()->second == 2.5);
123 assert(next(m.begin())->first == 5);
124 assert(next(m.begin())->second == 5.5);
125
126 i = m.erase(next(m.cbegin(), 0));
127 assert(m.size() == 1);
128 assert(i == next(m.begin(), 0));
129 assert(m.begin()->first == 5);
130 assert(m.begin()->second == 5.5);
131
132 i = m.erase(m.cbegin());
133 assert(m.size() == 0);
134 assert(i == m.begin());
135 assert(i == m.end());
136 }
137 #if __cplusplus >= 201103L
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(next(m.cbegin(), 3));
156 assert(m.size() == 7);
157 assert(i == next(m.begin(), 3));
158 assert(m.begin()->first == 1);
159 assert(m.begin()->second == 1.5);
160 assert(next(m.begin())->first == 2);
161 assert(next(m.begin())->second == 2.5);
162 assert(next(m.begin(), 2)->first == 3);
163 assert(next(m.begin(), 2)->second == 3.5);
164 assert(next(m.begin(), 3)->first == 5);
165 assert(next(m.begin(), 3)->second == 5.5);
166 assert(next(m.begin(), 4)->first == 6);
167 assert(next(m.begin(), 4)->second == 6.5);
168 assert(next(m.begin(), 5)->first == 7);
169 assert(next(m.begin(), 5)->second == 7.5);
170 assert(next(m.begin(), 6)->first == 8);
171 assert(next(m.begin(), 6)->second == 8.5);
172
173 i = m.erase(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(next(m.begin())->first == 3);
179 assert(next(m.begin())->second == 3.5);
180 assert(next(m.begin(), 2)->first == 5);
181 assert(next(m.begin(), 2)->second == 5.5);
182 assert(next(m.begin(), 3)->first == 6);
183 assert(next(m.begin(), 3)->second == 6.5);
184 assert(next(m.begin(), 4)->first == 7);
185 assert(next(m.begin(), 4)->second == 7.5);
186 assert(next(m.begin(), 5)->first == 8);
187 assert(next(m.begin(), 5)->second == 8.5);
188
189 i = m.erase(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(next(m.begin())->first == 3);
195 assert(next(m.begin())->second == 3.5);
196 assert(next(m.begin(), 2)->first == 5);
197 assert(next(m.begin(), 2)->second == 5.5);
198 assert(next(m.begin(), 3)->first == 6);
199 assert(next(m.begin(), 3)->second == 6.5);
200 assert(next(m.begin(), 4)->first == 7);
201 assert(next(m.begin(), 4)->second == 7.5);
202
203 i = m.erase(next(m.cbegin(), 1));
204 assert(m.size() == 4);
205 assert(i == next(m.begin()));
206 assert(m.begin()->first == 2);
207 assert(m.begin()->second == 2.5);
208 assert(next(m.begin())->first == 5);
209 assert(next(m.begin())->second == 5.5);
210 assert(next(m.begin(), 2)->first == 6);
211 assert(next(m.begin(), 2)->second == 6.5);
212 assert(next(m.begin(), 3)->first == 7);
213 assert(next(m.begin(), 3)->second == 7.5);
214
215 i = m.erase(next(m.cbegin(), 2));
216 assert(m.size() == 3);
217 assert(i == next(m.begin(), 2));
218 assert(m.begin()->first == 2);
219 assert(m.begin()->second == 2.5);
220 assert(next(m.begin())->first == 5);
221 assert(next(m.begin())->second == 5.5);
222 assert(next(m.begin(), 2)->first == 7);
223 assert(next(m.begin(), 2)->second == 7.5);
224
225 i = m.erase(next(m.cbegin(), 2));
226 assert(m.size() == 2);
227 assert(i == next(m.begin(), 2));
228 assert(m.begin()->first == 2);
229 assert(m.begin()->second == 2.5);
230 assert(next(m.begin())->first == 5);
231 assert(next(m.begin())->second == 5.5);
232
233 i = m.erase(next(m.cbegin(), 0));
234 assert(m.size() == 1);
235 assert(i == 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 __cplusplus >= 201402L
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