-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSet
More file actions
71 lines (55 loc) · 1.54 KB
/
Set
File metadata and controls
71 lines (55 loc) · 1.54 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* *********************************************************
* KNOWLEDGE CENTER
* std::set
* Detailed Video Explanation: https://youtu.be/2OEnAdl1eLc
********************************************************** */
#include <iostream>
#include <set>
#include <vector>
using namespace std;
class Student{
public:
int id;
string name;
void print_student() const {
cout << "[ name = " << name << ", id = " << id << "]\n";
}
bool operator < (const Student& other) const {
return (this->id > other.id);
}
};
int main() {
set<int> s = {10, 20, 5, 10, 15, 20, 4};
cout << "size = " << s.size() << endl;
s.insert(100);
s.insert(10);
cout << "size = " << s.size() << endl;
for(auto& el: s)
cout << el << " ";
cout << endl;
//auto it = s.erase(s.find(10));
//cout << *it << endl;
int num_erased = s.erase(10);
cout << "num_erased = " << num_erased << endl;
for(auto& el: s)
cout << el << " ";
cout << endl;
auto ub = s.upper_bound(10);
auto lb = s.lower_bound(10);
cout << "ub = " << *ub << endl;
cout << "lb = " << *lb << endl;
s.insert({-10, -30, -20});
for(auto& el: s)
cout << el << " ";
cout << endl;
vector<int> v = {10, 20, 15, 5, 4};
s.insert(v.begin(), v.end());
for(auto& el: s)
cout << el << " ";
cout << endl;
//------------
set<Student> sst = {{50, "Simon"}, {20, "Thomas"}};
for(auto& st: sst)
st.print_student();
return 0;
}