1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// <set>
10
11// class set
12
13// size_type erase(const key_type& k);
14
15#include <set>
16#include <cassert>
17
18#include "test_macros.h"
19#include "min_allocator.h"
20
21int main(int, char**)
22{
23 {
24 typedef std::set<int> M;
25 typedef int V;
26 typedef M::size_type 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(x: 9);
41 assert(m.size() == 8);
42 assert(i == 0);
43 assert(*std::next(m.begin(), 0) == 1);
44 assert(*std::next(m.begin(), 1) == 2);
45 assert(*std::next(m.begin(), 2) == 3);
46 assert(*std::next(m.begin(), 3) == 4);
47 assert(*std::next(m.begin(), 4) == 5);
48 assert(*std::next(m.begin(), 5) == 6);
49 assert(*std::next(m.begin(), 6) == 7);
50 assert(*std::next(m.begin(), 7) == 8);
51
52 i = m.erase(x: 4);
53 assert(m.size() == 7);
54 assert(i == 1);
55 assert(*std::next(m.begin(), 0) == 1);
56 assert(*std::next(m.begin(), 1) == 2);
57 assert(*std::next(m.begin(), 2) == 3);
58 assert(*std::next(m.begin(), 3) == 5);
59 assert(*std::next(m.begin(), 4) == 6);
60 assert(*std::next(m.begin(), 5) == 7);
61 assert(*std::next(m.begin(), 6) == 8);
62
63 i = m.erase(x: 1);
64 assert(m.size() == 6);
65 assert(i == 1);
66 assert(*std::next(m.begin(), 0) == 2);
67 assert(*std::next(m.begin(), 1) == 3);
68 assert(*std::next(m.begin(), 2) == 5);
69 assert(*std::next(m.begin(), 3) == 6);
70 assert(*std::next(m.begin(), 4) == 7);
71 assert(*std::next(m.begin(), 5) == 8);
72
73 i = m.erase(x: 8);
74 assert(m.size() == 5);
75 assert(i == 1);
76 assert(*std::next(m.begin(), 0) == 2);
77 assert(*std::next(m.begin(), 1) == 3);
78 assert(*std::next(m.begin(), 2) == 5);
79 assert(*std::next(m.begin(), 3) == 6);
80 assert(*std::next(m.begin(), 4) == 7);
81
82 i = m.erase(x: 3);
83 assert(m.size() == 4);
84 assert(i == 1);
85 assert(*std::next(m.begin(), 0) == 2);
86 assert(*std::next(m.begin(), 1) == 5);
87 assert(*std::next(m.begin(), 2) == 6);
88 assert(*std::next(m.begin(), 3) == 7);
89
90 i = m.erase(x: 6);
91 assert(m.size() == 3);
92 assert(i == 1);
93 assert(*std::next(m.begin(), 0) == 2);
94 assert(*std::next(m.begin(), 1) == 5);
95 assert(*std::next(m.begin(), 2) == 7);
96
97 i = m.erase(x: 7);
98 assert(m.size() == 2);
99 assert(i == 1);
100 assert(*std::next(m.begin(), 0) == 2);
101 assert(*std::next(m.begin(), 1) == 5);
102
103 i = m.erase(x: 2);
104 assert(m.size() == 1);
105 assert(i == 1);
106 assert(*std::next(m.begin(), 0) == 5);
107
108 i = m.erase(x: 5);
109 assert(m.size() == 0);
110 assert(i == 1);
111 }
112#if TEST_STD_VER >= 11
113 {
114 typedef std::set<int, std::less<int>, min_allocator<int>> M;
115 typedef int V;
116 typedef M::size_type I;
117 V ar[] =
118 {
119 1,
120 2,
121 3,
122 4,
123 5,
124 6,
125 7,
126 8
127 };
128 M m(ar, ar + sizeof(ar)/sizeof(ar[0]));
129 assert(m.size() == 8);
130 I i = m.erase(9);
131 assert(m.size() == 8);
132 assert(i == 0);
133 assert(*std::next(m.begin(), 0) == 1);
134 assert(*std::next(m.begin(), 1) == 2);
135 assert(*std::next(m.begin(), 2) == 3);
136 assert(*std::next(m.begin(), 3) == 4);
137 assert(*std::next(m.begin(), 4) == 5);
138 assert(*std::next(m.begin(), 5) == 6);
139 assert(*std::next(m.begin(), 6) == 7);
140 assert(*std::next(m.begin(), 7) == 8);
141
142 i = m.erase(4);
143 assert(m.size() == 7);
144 assert(i == 1);
145 assert(*std::next(m.begin(), 0) == 1);
146 assert(*std::next(m.begin(), 1) == 2);
147 assert(*std::next(m.begin(), 2) == 3);
148 assert(*std::next(m.begin(), 3) == 5);
149 assert(*std::next(m.begin(), 4) == 6);
150 assert(*std::next(m.begin(), 5) == 7);
151 assert(*std::next(m.begin(), 6) == 8);
152
153 i = m.erase(1);
154 assert(m.size() == 6);
155 assert(i == 1);
156 assert(*std::next(m.begin(), 0) == 2);
157 assert(*std::next(m.begin(), 1) == 3);
158 assert(*std::next(m.begin(), 2) == 5);
159 assert(*std::next(m.begin(), 3) == 6);
160 assert(*std::next(m.begin(), 4) == 7);
161 assert(*std::next(m.begin(), 5) == 8);
162
163 i = m.erase(8);
164 assert(m.size() == 5);
165 assert(i == 1);
166 assert(*std::next(m.begin(), 0) == 2);
167 assert(*std::next(m.begin(), 1) == 3);
168 assert(*std::next(m.begin(), 2) == 5);
169 assert(*std::next(m.begin(), 3) == 6);
170 assert(*std::next(m.begin(), 4) == 7);
171
172 i = m.erase(3);
173 assert(m.size() == 4);
174 assert(i == 1);
175 assert(*std::next(m.begin(), 0) == 2);
176 assert(*std::next(m.begin(), 1) == 5);
177 assert(*std::next(m.begin(), 2) == 6);
178 assert(*std::next(m.begin(), 3) == 7);
179
180 i = m.erase(6);
181 assert(m.size() == 3);
182 assert(i == 1);
183 assert(*std::next(m.begin(), 0) == 2);
184 assert(*std::next(m.begin(), 1) == 5);
185 assert(*std::next(m.begin(), 2) == 7);
186
187 i = m.erase(7);
188 assert(m.size() == 2);
189 assert(i == 1);
190 assert(*std::next(m.begin(), 0) == 2);
191 assert(*std::next(m.begin(), 1) == 5);
192
193 i = m.erase(2);
194 assert(m.size() == 1);
195 assert(i == 1);
196 assert(*std::next(m.begin(), 0) == 5);
197
198 i = m.erase(5);
199 assert(m.size() == 0);
200 assert(i == 1);
201 }
202#endif
203
204 return 0;
205}
206

source code of libcxx/test/std/containers/associative/set/erase_key.pass.cpp