-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.cpp
More file actions
198 lines (185 loc) · 4.39 KB
/
Set.cpp
File metadata and controls
198 lines (185 loc) · 4.39 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#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";
}
// #include<iostream>
// #include<bits/stdc++.h>
// using namespace std;
// //sets is like a vector but to store unique elements vector allows duplicate sets allow unique
// //in set to insert s.insert(5) s.insert(10)
// //s.insert(7) it will be in order 5 7 10
// //insert in a sorted order and dont allow duplicate
// int main()
// {
// set<char> a;
// a.insert('G');
// a.insert('F');
// a.insert('G');
// //a.begin()
// //a.end()
// for(auto& str: a) // auto iterator also used to iteratre map
// {
// cout<<str<<' ';
// // F G
// }
// cout<<a.size()<<'\n';
// cout<<a.empty()<<'\n';
// //a.find()
// if(a.find('G')!=a.end())
// {
// cout<<"G is present";
// }
// else
// {
// cout<<"G is not present";
// }
// //count how many times a particular element present
// cout<<a.count('G')<<'\n';
// cout<<a.count('A')<<'\n';
// a.clear();
// //insert -> O(log n) and pq ->O(log n) other dsa O(1)
// }