-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSets.cpp
More file actions
148 lines (148 loc) · 3.37 KB
/
Sets.cpp
File metadata and controls
148 lines (148 loc) · 3.37 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <iostream>
#include <set>
using namespace std;
int main()
{
// first it is in ascending order
set<int> s;
s.insert(10);
s.insert(5);
s.insert(36);
s.insert(1);
s.insert(4);
for (int x : s)
cout << x << " ";
cout << "\n";
// to make descending;
set<int, greater<int>> st;
st.insert(10);
st.insert(5);
st.insert(36);
st.insert(1);
st.insert(4);
for (int x : st)
cout << x << " ";
cout << "\n";
// traversing
set<int, greater<int>> st1;
st1.insert(10);
st1.insert(5);
st1.insert(36);
st1.insert(1);
st1.insert(4);
for (auto it = st1.begin(); it != st1.end(); it++)
cout << (*it) << " ";
cout << "\n";
// reverse iterator;
set<int, greater<int>> st2;
st2.insert(10);
st2.insert(5);
st2.insert(36);
st2.insert(1);
st2.insert(4);
for (auto it = st2.rbegin(); it != st2.rend(); it++)
cout << (*it) << " ";
cout << "\n";
// ignores duplicate
set<int, greater<int>> st3;
st3.insert(10);
st3.insert(5);
st3.insert(10);
st3.insert(10);
st3.insert(5);
for (int x : st3)
cout << x << " ";
cout << "\n";
// find function search the element return iterator
set<int, greater<int>> st4;
st4.insert(10);
st4.insert(5);
st4.insert(101);
st4.insert(1);
st4.insert(532);
auto it = st4.find(101);
if (it == st4.end())
cout << "Not found\n";
else
{
cout << "Found\n";
cout << (*it);
}
cout << "\n";
// clear function
set<int, greater<int>> st5;
st5.insert(101);
st5.insert(5);
st5.insert(115);
st5.insert(14210);
st5.insert(543);
cout << "size of the set is : " << st5.size() << "\n";
st5.clear();
cout << "size of the set is : " << st5.size();
cout << "\n";
// count function used in place of find
set<int, greater<int>> st6;
st6.insert(10);
st6.insert(5);
st6.insert(10);
st6.insert(10);
st6.insert(5);
if (st6.count(10))
cout << "Found";
else
cout << "Not Found";
cout << "\n";
// erase function used to remove elements from a set
set<int, greater<int>> st7;
st7.insert(10);
st7.insert(5);
st7.insert(4);
st7.insert(34);
st7.insert(86);
st7.erase(5);
auto iter = st7.find(34);
st7.erase(iter);
for (int x : st7)
cout << x << " ";
cout << "\n";
// erase in a range
st7.insert(41);
st7.insert(42);
st7.insert(43);
st7.insert(44);
st7.insert(45);
for (int x : st7)
cout << x << " ";
cout << "\n";
auto iter1 = st7.find(42);
st7.erase(iter1, st7.end());
for (int x : st7)
cout << x << " ";
cout << "\n";
// lower_bound
set<int> st8;
st8.insert(10);
st8.insert(5);
st8.insert(36);
st8.insert(1);
st8.insert(4);
auto iter2 = st8.lower_bound(2);
if (iter2 != st8.end())
cout << (*iter2) << " "
<< "\n";
else
cout << "given element is greater than the largest";
// upper_bound
set<int> st9;
st9.insert(10);
st9.insert(5);
st9.insert(36);
st9.insert(1);
st9.insert(4);
auto iter3 = st9.upper_bound(5);
if (iter3 != st9.end())
cout << (*iter3) << " "
<< "\n";
else
cout << "given element is greater than the largest";
}