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 set
13 
14 // iterator erase(const_iterator position);
15 
16 #include <set>
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::set<int> M;
33         typedef int V;
34         typedef M::iterator I;
35         V ar[] =
36         {
37             1,
38             2,
39             3,
40             4,
41             5,
42             6,
43             7,
44             8
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(*next(m.begin(), 0) == 1);
52         assert(*next(m.begin(), 1) == 2);
53         assert(*next(m.begin(), 2) == 3);
54         assert(*next(m.begin(), 3) == 5);
55         assert(*next(m.begin(), 4) == 6);
56         assert(*next(m.begin(), 5) == 7);
57         assert(*next(m.begin(), 6) == 8);
58 
59         i = m.erase(next(m.cbegin(), 0));
60         assert(m.size() == 6);
61         assert(i == m.begin());
62         assert(*next(m.begin(), 0) == 2);
63         assert(*next(m.begin(), 1) == 3);
64         assert(*next(m.begin(), 2) == 5);
65         assert(*next(m.begin(), 3) == 6);
66         assert(*next(m.begin(), 4) == 7);
67         assert(*next(m.begin(), 5) == 8);
68 
69         i = m.erase(next(m.cbegin(), 5));
70         assert(m.size() == 5);
71         assert(i == m.end());
72         assert(*next(m.begin(), 0) == 2);
73         assert(*next(m.begin(), 1) == 3);
74         assert(*next(m.begin(), 2) == 5);
75         assert(*next(m.begin(), 3) == 6);
76         assert(*next(m.begin(), 4) == 7);
77 
78         i = m.erase(next(m.cbegin(), 1));
79         assert(m.size() == 4);
80         assert(i == next(m.begin()));
81         assert(*next(m.begin(), 0) == 2);
82         assert(*next(m.begin(), 1) == 5);
83         assert(*next(m.begin(), 2) == 6);
84         assert(*next(m.begin(), 3) == 7);
85 
86         i = m.erase(next(m.cbegin(), 2));
87         assert(m.size() == 3);
88         assert(i == next(m.begin(), 2));
89         assert(*next(m.begin(), 0) == 2);
90         assert(*next(m.begin(), 1) == 5);
91         assert(*next(m.begin(), 2) == 7);
92 
93         i = m.erase(next(m.cbegin(), 2));
94         assert(m.size() == 2);
95         assert(i == next(m.begin(), 2));
96         assert(*next(m.begin(), 0) == 2);
97         assert(*next(m.begin(), 1) == 5);
98 
99         i = m.erase(next(m.cbegin(), 0));
100         assert(m.size() == 1);
101         assert(i == next(m.begin(), 0));
102         assert(*next(m.begin(), 0) == 5);
103 
104         i = m.erase(m.cbegin());
105         assert(m.size() == 0);
106         assert(i == m.begin());
107         assert(i == m.end());
108     }
109 #if __cplusplus >= 201103L
110     {
111         typedef std::set<int, std::less<int>, min_allocator<int>> M;
112         typedef int V;
113         typedef M::iterator I;
114         V ar[] =
115         {
116             1,
117             2,
118             3,
119             4,
120             5,
121             6,
122             7,
123             8
124         };
125         M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
126         assert(m.size() == 8);
127         I i = m.erase(next(m.cbegin(), 3));
128         assert(m.size() == 7);
129         assert(i == next(m.begin(), 3));
130         assert(*next(m.begin(), 0) == 1);
131         assert(*next(m.begin(), 1) == 2);
132         assert(*next(m.begin(), 2) == 3);
133         assert(*next(m.begin(), 3) == 5);
134         assert(*next(m.begin(), 4) == 6);
135         assert(*next(m.begin(), 5) == 7);
136         assert(*next(m.begin(), 6) == 8);
137 
138         i = m.erase(next(m.cbegin(), 0));
139         assert(m.size() == 6);
140         assert(i == m.begin());
141         assert(*next(m.begin(), 0) == 2);
142         assert(*next(m.begin(), 1) == 3);
143         assert(*next(m.begin(), 2) == 5);
144         assert(*next(m.begin(), 3) == 6);
145         assert(*next(m.begin(), 4) == 7);
146         assert(*next(m.begin(), 5) == 8);
147 
148         i = m.erase(next(m.cbegin(), 5));
149         assert(m.size() == 5);
150         assert(i == m.end());
151         assert(*next(m.begin(), 0) == 2);
152         assert(*next(m.begin(), 1) == 3);
153         assert(*next(m.begin(), 2) == 5);
154         assert(*next(m.begin(), 3) == 6);
155         assert(*next(m.begin(), 4) == 7);
156 
157         i = m.erase(next(m.cbegin(), 1));
158         assert(m.size() == 4);
159         assert(i == next(m.begin()));
160         assert(*next(m.begin(), 0) == 2);
161         assert(*next(m.begin(), 1) == 5);
162         assert(*next(m.begin(), 2) == 6);
163         assert(*next(m.begin(), 3) == 7);
164 
165         i = m.erase(next(m.cbegin(), 2));
166         assert(m.size() == 3);
167         assert(i == next(m.begin(), 2));
168         assert(*next(m.begin(), 0) == 2);
169         assert(*next(m.begin(), 1) == 5);
170         assert(*next(m.begin(), 2) == 7);
171 
172         i = m.erase(next(m.cbegin(), 2));
173         assert(m.size() == 2);
174         assert(i == next(m.begin(), 2));
175         assert(*next(m.begin(), 0) == 2);
176         assert(*next(m.begin(), 1) == 5);
177 
178         i = m.erase(next(m.cbegin(), 0));
179         assert(m.size() == 1);
180         assert(i == next(m.begin(), 0));
181         assert(*next(m.begin(), 0) == 5);
182 
183         i = m.erase(m.cbegin());
184         assert(m.size() == 0);
185         assert(i == m.begin());
186         assert(i == m.end());
187     }
188 #endif
189 #if __cplusplus >= 201402L
190     {
191     //  This is LWG #2059
192         typedef TemplateConstructor T;
193         typedef std::set<T> C;
194         typedef C::iterator I;
195 
196         C c;
197         T a{0};
198         I it = c.find(a);
199         if (it != c.end())
200             c.erase(it);
201     }
202 #endif
203 }
204