-
-
Notifications
You must be signed in to change notification settings - Fork 617
Expand file tree
/
Copy pathset.cpp
More file actions
39 lines (31 loc) · 978 Bytes
/
set.cpp
File metadata and controls
39 lines (31 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
#include <cassert>
#include <set>
int main() {
std::set<int> s{0, 2};
// Insert another element.
auto insert_ret = s.insert(3);
assert(insert_ret.first == s.find(3));
assert(insert_ret.second == true);
// Insert something already there, second of return is false.
insert_ret = s.insert(3);
assert(insert_ret.second == false);
// Check if a value is present.
auto zero = s.find(0);
assert(zero != s.end());
assert(*zero == 0);
// Now for a value that is not present.
auto one = s.find(1);
assert(one == s.end());
#if __cplusplus > 201703L
// In C++20 we finally have a .contains() to avoid the end mess!
assert(s.contains(0));
assert(!s.contains(1));
#endif
// Remove an element.
// Was present, so return 1;
assert(s.erase(0) == 1);
assert(s.find(0) == s.end());
// Not present, return 0.
assert(s.erase(0) == 0);
}