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 multimap
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::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(next(m.cbegin(), 3));
50         assert(m.size() == 8);
51         assert(i == next(m.begin(), 3));
52         assert(m.begin()->first == 1);
53         assert(m.begin()->second == 1);
54         assert(next(m.begin())->first == 1);
55         assert(next(m.begin())->second == 1.5);
56         assert(next(m.begin(), 2)->first == 1);
57         assert(next(m.begin(), 2)->second == 2);
58         assert(next(m.begin(), 3)->first == 2);
59         assert(next(m.begin(), 3)->second == 1.5);
60         assert(next(m.begin(), 4)->first == 2);
61         assert(next(m.begin(), 4)->second == 2);
62         assert(next(m.begin(), 5)->first == 3);
63         assert(next(m.begin(), 5)->second == 1);
64         assert(next(m.begin(), 6)->first == 3);
65         assert(next(m.begin(), 6)->second == 1.5);
66         assert(next(m.begin(), 7)->first == 3);
67         assert(next(m.begin(), 7)->second == 2);
68 
69         i = m.erase(next(m.cbegin(), 0));
70         assert(m.size() == 7);
71         assert(i == m.begin());
72         assert(next(m.begin(), 0)->first == 1);
73         assert(next(m.begin(), 0)->second == 1.5);
74         assert(next(m.begin(), 1)->first == 1);
75         assert(next(m.begin(), 1)->second == 2);
76         assert(next(m.begin(), 2)->first == 2);
77         assert(next(m.begin(), 2)->second == 1.5);
78         assert(next(m.begin(), 3)->first == 2);
79         assert(next(m.begin(), 3)->second == 2);
80         assert(next(m.begin(), 4)->first == 3);
81         assert(next(m.begin(), 4)->second == 1);
82         assert(next(m.begin(), 5)->first == 3);
83         assert(next(m.begin(), 5)->second == 1.5);
84         assert(next(m.begin(), 6)->first == 3);
85         assert(next(m.begin(), 6)->second == 2);
86 
87         i = m.erase(next(m.cbegin(), 5));
88         assert(m.size() == 6);
89         assert(i == prev(m.end()));
90         assert(next(m.begin(), 0)->first == 1);
91         assert(next(m.begin(), 0)->second == 1.5);
92         assert(next(m.begin(), 1)->first == 1);
93         assert(next(m.begin(), 1)->second == 2);
94         assert(next(m.begin(), 2)->first == 2);
95         assert(next(m.begin(), 2)->second == 1.5);
96         assert(next(m.begin(), 3)->first == 2);
97         assert(next(m.begin(), 3)->second == 2);
98         assert(next(m.begin(), 4)->first == 3);
99         assert(next(m.begin(), 4)->second == 1);
100         assert(next(m.begin(), 5)->first == 3);
101         assert(next(m.begin(), 5)->second == 2);
102 
103         i = m.erase(next(m.cbegin(), 1));
104         assert(m.size() == 5);
105         assert(i == next(m.begin()));
106         assert(next(m.begin(), 0)->first == 1);
107         assert(next(m.begin(), 0)->second == 1.5);
108         assert(next(m.begin(), 1)->first == 2);
109         assert(next(m.begin(), 1)->second == 1.5);
110         assert(next(m.begin(), 2)->first == 2);
111         assert(next(m.begin(), 2)->second == 2);
112         assert(next(m.begin(), 3)->first == 3);
113         assert(next(m.begin(), 3)->second == 1);
114         assert(next(m.begin(), 4)->first == 3);
115         assert(next(m.begin(), 4)->second == 2);
116 
117         i = m.erase(next(m.cbegin(), 2));
118         assert(m.size() == 4);
119         assert(i == next(m.begin(), 2));
120         assert(next(m.begin(), 0)->first == 1);
121         assert(next(m.begin(), 0)->second == 1.5);
122         assert(next(m.begin(), 1)->first == 2);
123         assert(next(m.begin(), 1)->second == 1.5);
124         assert(next(m.begin(), 2)->first == 3);
125         assert(next(m.begin(), 2)->second == 1);
126         assert(next(m.begin(), 3)->first == 3);
127         assert(next(m.begin(), 3)->second == 2);
128 
129         i = m.erase(next(m.cbegin(), 2));
130         assert(m.size() == 3);
131         assert(i == next(m.begin(), 2));
132         assert(next(m.begin(), 0)->first == 1);
133         assert(next(m.begin(), 0)->second == 1.5);
134         assert(next(m.begin(), 1)->first == 2);
135         assert(next(m.begin(), 1)->second == 1.5);
136         assert(next(m.begin(), 2)->first == 3);
137         assert(next(m.begin(), 2)->second == 2);
138 
139         i = m.erase(next(m.cbegin(), 0));
140         assert(m.size() == 2);
141         assert(i == next(m.begin(), 0));
142         assert(next(m.begin(), 0)->first == 2);
143         assert(next(m.begin(), 0)->second == 1.5);
144         assert(next(m.begin(), 1)->first == 3);
145         assert(next(m.begin(), 1)->second == 2);
146 
147         i = m.erase(next(m.cbegin(), 1));
148         assert(m.size() == 1);
149         assert(i == m.end());
150         assert(next(m.begin(), 0)->first == 2);
151         assert(next(m.begin(), 0)->second == 1.5);
152 
153         i = m.erase(m.cbegin());
154         assert(m.size() == 0);
155         assert(i == m.begin());
156         assert(i == m.end());
157     }
158 #if __cplusplus >= 201103L
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(next(m.cbegin(), 3));
178         assert(m.size() == 8);
179         assert(i == next(m.begin(), 3));
180         assert(m.begin()->first == 1);
181         assert(m.begin()->second == 1);
182         assert(next(m.begin())->first == 1);
183         assert(next(m.begin())->second == 1.5);
184         assert(next(m.begin(), 2)->first == 1);
185         assert(next(m.begin(), 2)->second == 2);
186         assert(next(m.begin(), 3)->first == 2);
187         assert(next(m.begin(), 3)->second == 1.5);
188         assert(next(m.begin(), 4)->first == 2);
189         assert(next(m.begin(), 4)->second == 2);
190         assert(next(m.begin(), 5)->first == 3);
191         assert(next(m.begin(), 5)->second == 1);
192         assert(next(m.begin(), 6)->first == 3);
193         assert(next(m.begin(), 6)->second == 1.5);
194         assert(next(m.begin(), 7)->first == 3);
195         assert(next(m.begin(), 7)->second == 2);
196 
197         i = m.erase(next(m.cbegin(), 0));
198         assert(m.size() == 7);
199         assert(i == m.begin());
200         assert(next(m.begin(), 0)->first == 1);
201         assert(next(m.begin(), 0)->second == 1.5);
202         assert(next(m.begin(), 1)->first == 1);
203         assert(next(m.begin(), 1)->second == 2);
204         assert(next(m.begin(), 2)->first == 2);
205         assert(next(m.begin(), 2)->second == 1.5);
206         assert(next(m.begin(), 3)->first == 2);
207         assert(next(m.begin(), 3)->second == 2);
208         assert(next(m.begin(), 4)->first == 3);
209         assert(next(m.begin(), 4)->second == 1);
210         assert(next(m.begin(), 5)->first == 3);
211         assert(next(m.begin(), 5)->second == 1.5);
212         assert(next(m.begin(), 6)->first == 3);
213         assert(next(m.begin(), 6)->second == 2);
214 
215         i = m.erase(next(m.cbegin(), 5));
216         assert(m.size() == 6);
217         assert(i == prev(m.end()));
218         assert(next(m.begin(), 0)->first == 1);
219         assert(next(m.begin(), 0)->second == 1.5);
220         assert(next(m.begin(), 1)->first == 1);
221         assert(next(m.begin(), 1)->second == 2);
222         assert(next(m.begin(), 2)->first == 2);
223         assert(next(m.begin(), 2)->second == 1.5);
224         assert(next(m.begin(), 3)->first == 2);
225         assert(next(m.begin(), 3)->second == 2);
226         assert(next(m.begin(), 4)->first == 3);
227         assert(next(m.begin(), 4)->second == 1);
228         assert(next(m.begin(), 5)->first == 3);
229         assert(next(m.begin(), 5)->second == 2);
230 
231         i = m.erase(next(m.cbegin(), 1));
232         assert(m.size() == 5);
233         assert(i == next(m.begin()));
234         assert(next(m.begin(), 0)->first == 1);
235         assert(next(m.begin(), 0)->second == 1.5);
236         assert(next(m.begin(), 1)->first == 2);
237         assert(next(m.begin(), 1)->second == 1.5);
238         assert(next(m.begin(), 2)->first == 2);
239         assert(next(m.begin(), 2)->second == 2);
240         assert(next(m.begin(), 3)->first == 3);
241         assert(next(m.begin(), 3)->second == 1);
242         assert(next(m.begin(), 4)->first == 3);
243         assert(next(m.begin(), 4)->second == 2);
244 
245         i = m.erase(next(m.cbegin(), 2));
246         assert(m.size() == 4);
247         assert(i == next(m.begin(), 2));
248         assert(next(m.begin(), 0)->first == 1);
249         assert(next(m.begin(), 0)->second == 1.5);
250         assert(next(m.begin(), 1)->first == 2);
251         assert(next(m.begin(), 1)->second == 1.5);
252         assert(next(m.begin(), 2)->first == 3);
253         assert(next(m.begin(), 2)->second == 1);
254         assert(next(m.begin(), 3)->first == 3);
255         assert(next(m.begin(), 3)->second == 2);
256 
257         i = m.erase(next(m.cbegin(), 2));
258         assert(m.size() == 3);
259         assert(i == next(m.begin(), 2));
260         assert(next(m.begin(), 0)->first == 1);
261         assert(next(m.begin(), 0)->second == 1.5);
262         assert(next(m.begin(), 1)->first == 2);
263         assert(next(m.begin(), 1)->second == 1.5);
264         assert(next(m.begin(), 2)->first == 3);
265         assert(next(m.begin(), 2)->second == 2);
266 
267         i = m.erase(next(m.cbegin(), 0));
268         assert(m.size() == 2);
269         assert(i == next(m.begin(), 0));
270         assert(next(m.begin(), 0)->first == 2);
271         assert(next(m.begin(), 0)->second == 1.5);
272         assert(next(m.begin(), 1)->first == 3);
273         assert(next(m.begin(), 1)->second == 2);
274 
275         i = m.erase(next(m.cbegin(), 1));
276         assert(m.size() == 1);
277         assert(i == m.end());
278         assert(next(m.begin(), 0)->first == 2);
279         assert(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 __cplusplus >= 201402L
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