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