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 // <set>
11 
12 // class multiset
13 
14 // iterator erase(const_iterator position);
15 
16 #include <set>
17 #include <cassert>
18 
19 #include "min_allocator.h"
20 
main()21 int main()
22 {
23     {
24         typedef std::multiset<int> M;
25         typedef int V;
26         typedef M::iterator I;
27         V ar[] =
28         {
29             1,
30             2,
31             3,
32             4,
33             5,
34             6,
35             7,
36             8
37         };
38         M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
39         assert(m.size() == 8);
40         I i = m.erase(next(m.cbegin(), 3));
41         assert(m.size() == 7);
42         assert(i == next(m.begin(), 3));
43         assert(*next(m.begin(), 0) == 1);
44         assert(*next(m.begin(), 1) == 2);
45         assert(*next(m.begin(), 2) == 3);
46         assert(*next(m.begin(), 3) == 5);
47         assert(*next(m.begin(), 4) == 6);
48         assert(*next(m.begin(), 5) == 7);
49         assert(*next(m.begin(), 6) == 8);
50 
51         i = m.erase(next(m.cbegin(), 0));
52         assert(m.size() == 6);
53         assert(i == m.begin());
54         assert(*next(m.begin(), 0) == 2);
55         assert(*next(m.begin(), 1) == 3);
56         assert(*next(m.begin(), 2) == 5);
57         assert(*next(m.begin(), 3) == 6);
58         assert(*next(m.begin(), 4) == 7);
59         assert(*next(m.begin(), 5) == 8);
60 
61         i = m.erase(next(m.cbegin(), 5));
62         assert(m.size() == 5);
63         assert(i == m.end());
64         assert(*next(m.begin(), 0) == 2);
65         assert(*next(m.begin(), 1) == 3);
66         assert(*next(m.begin(), 2) == 5);
67         assert(*next(m.begin(), 3) == 6);
68         assert(*next(m.begin(), 4) == 7);
69 
70         i = m.erase(next(m.cbegin(), 1));
71         assert(m.size() == 4);
72         assert(i == next(m.begin()));
73         assert(*next(m.begin(), 0) == 2);
74         assert(*next(m.begin(), 1) == 5);
75         assert(*next(m.begin(), 2) == 6);
76         assert(*next(m.begin(), 3) == 7);
77 
78         i = m.erase(next(m.cbegin(), 2));
79         assert(m.size() == 3);
80         assert(i == next(m.begin(), 2));
81         assert(*next(m.begin(), 0) == 2);
82         assert(*next(m.begin(), 1) == 5);
83         assert(*next(m.begin(), 2) == 7);
84 
85         i = m.erase(next(m.cbegin(), 2));
86         assert(m.size() == 2);
87         assert(i == next(m.begin(), 2));
88         assert(*next(m.begin(), 0) == 2);
89         assert(*next(m.begin(), 1) == 5);
90 
91         i = m.erase(next(m.cbegin(), 0));
92         assert(m.size() == 1);
93         assert(i == next(m.begin(), 0));
94         assert(*next(m.begin(), 0) == 5);
95 
96         i = m.erase(m.cbegin());
97         assert(m.size() == 0);
98         assert(i == m.begin());
99         assert(i == m.end());
100     }
101 #if __cplusplus >= 201103L
102     {
103         typedef std::multiset<int, std::less<int>, min_allocator<int>> M;
104         typedef int V;
105         typedef M::iterator I;
106         V ar[] =
107         {
108             1,
109             2,
110             3,
111             4,
112             5,
113             6,
114             7,
115             8
116         };
117         M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
118         assert(m.size() == 8);
119         I i = m.erase(next(m.cbegin(), 3));
120         assert(m.size() == 7);
121         assert(i == next(m.begin(), 3));
122         assert(*next(m.begin(), 0) == 1);
123         assert(*next(m.begin(), 1) == 2);
124         assert(*next(m.begin(), 2) == 3);
125         assert(*next(m.begin(), 3) == 5);
126         assert(*next(m.begin(), 4) == 6);
127         assert(*next(m.begin(), 5) == 7);
128         assert(*next(m.begin(), 6) == 8);
129 
130         i = m.erase(next(m.cbegin(), 0));
131         assert(m.size() == 6);
132         assert(i == m.begin());
133         assert(*next(m.begin(), 0) == 2);
134         assert(*next(m.begin(), 1) == 3);
135         assert(*next(m.begin(), 2) == 5);
136         assert(*next(m.begin(), 3) == 6);
137         assert(*next(m.begin(), 4) == 7);
138         assert(*next(m.begin(), 5) == 8);
139 
140         i = m.erase(next(m.cbegin(), 5));
141         assert(m.size() == 5);
142         assert(i == m.end());
143         assert(*next(m.begin(), 0) == 2);
144         assert(*next(m.begin(), 1) == 3);
145         assert(*next(m.begin(), 2) == 5);
146         assert(*next(m.begin(), 3) == 6);
147         assert(*next(m.begin(), 4) == 7);
148 
149         i = m.erase(next(m.cbegin(), 1));
150         assert(m.size() == 4);
151         assert(i == next(m.begin()));
152         assert(*next(m.begin(), 0) == 2);
153         assert(*next(m.begin(), 1) == 5);
154         assert(*next(m.begin(), 2) == 6);
155         assert(*next(m.begin(), 3) == 7);
156 
157         i = m.erase(next(m.cbegin(), 2));
158         assert(m.size() == 3);
159         assert(i == next(m.begin(), 2));
160         assert(*next(m.begin(), 0) == 2);
161         assert(*next(m.begin(), 1) == 5);
162         assert(*next(m.begin(), 2) == 7);
163 
164         i = m.erase(next(m.cbegin(), 2));
165         assert(m.size() == 2);
166         assert(i == next(m.begin(), 2));
167         assert(*next(m.begin(), 0) == 2);
168         assert(*next(m.begin(), 1) == 5);
169 
170         i = m.erase(next(m.cbegin(), 0));
171         assert(m.size() == 1);
172         assert(i == next(m.begin(), 0));
173         assert(*next(m.begin(), 0) == 5);
174 
175         i = m.erase(m.cbegin());
176         assert(m.size() == 0);
177         assert(i == m.begin());
178         assert(i == m.end());
179     }
180 #endif
181 }
182