-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSET .cpp
192 lines (117 loc) · 3.64 KB
/
SET .cpp
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
#include<bits/stdc++.h>
using namespace std;
void print(set<int> x){
set <int>::iterator itr;
for(itr = x.begin(); itr != x.end(); itr++){
cout << *itr << " ";
}
cout << '\n';
}
int main(){
//--------------S E T--------------//
// -> A set is a container that stores unique element following a specific order
// Declaring a set
set <int> a;
// empty() -> Returns whether the set is empty or not
if(a.empty()){
cout << "Set empty" << '\n';
}
// inserting elements in random order
a.insert(60);
a.insert(40);
a.insert(10);
a.insert(80);
a.insert(50);
a.insert(20);
a.insert(30);
// inserting 30 again, but only one 30 will be added to the set
a.insert(30);
// Printing set
set <int>::iterator it;
// The elements will be stored in ascending order
// OUTPUT -> 10 20 30 40 50 60 80
cout << "Elements of set are : ";
for(it = a.begin(); it != a.end(); it++){
cout << *it << " ";
}
cout << '\n';
// size()
cout << "Size of set A -> ";
cout << a.size() << '\n';
// OUTPUT -> 7
// To store the elements in descending order
set <int, greater<int>> b;
b.insert(60);
b.insert(40);
b.insert(10);
b.insert(80);
b.insert(50);
b.insert(20);
b.insert(30);
b.insert(30);
// OUTPUT -> 80 60 50 40 30 20 10
cout << "Elements of set B in descending order are : ";
for(it = b.begin(); it != b.end(); it++){
cout << *it << " ";
}
cout << '\n';
// Assigning the elements from A to C
set <int> c(a.begin(), a.end());
cout << "Elements of set C are : ";
print(c);
// OUTPUT -> 10 20 30 40 50 60 80
// Remove all elements up to 40 in C
c.erase(c.begin(), c.find(40));
cout << "Remove all elements up to 40 in C : ";
print(c);
// OUTPUT -> 40 50 60 80
// Remove specific element in C
c.erase(50);
cout << "Remove specific element in C : ";
print(c);
//Before -> 40 50 60 80
//After -> 40 60 80
cout << "Count : \n";
// Count() -> returns 1 if element is present in the set
// returns 0 if element is not present in the set
//set A -> 10 20 30 40 50 60 80
cout << a.count(50) << '\n'; // 50 is present so OUTPUT -> 1
cout << a.count(100) << '\n'; // 100 is not present so OUTPUT -> 0
// emplace() -> used to insert a new element into the set, only if the element to be inserted is unique and does not already exists in the set.
// set C elements -> 40 60 80
// emplace 30 <- doesn't exit
// emplace 80 <- already exists
c.emplace(30);
c.emplace(80);
cout << "emplace() : ";
print(c); // OUTPUT -> 30 40 60 80
// array to set
int arr[]={12,75,10,32,20,25};
set <int> d (arr,arr+6); // 10,12,20,25,32,75
cout << "array to set : ";
print(d);
//OUTPUT -> 10 12 20 25 32 75
// swap() -> Exchanges the content of the set
// set C -> 30 40 60 80
// set D -> 10 12 20 25 32 75
c.swap(d);
cout << "swap() : \n";
print(c); // OUTPUT -> 10 12 20 25 32 75
print(d); // OUTPUT -> 30 40 60 80
// find() -> searching an element within a set
// set A -> 10 20 30 40 50 60 80
int val = 50;
if(a.find(val) != a.end())
cout<< "The set contains "<< val << '\n';
else
cout<< "The set does not contains "<< val << '\n';
// Copy set to vector
//set A -> 10 20 30 40 50 60 80
vector <int> v(a.size()); //a.size() is used to reserve enough space in the vector to hold the content of set A
copy(a.begin(), a.end(), v.begin());
cout << "Copy set A to vector V : "; // OUTPUT -> 10 20 30 40 50 60 80
for(auto i:v){
cout << i << " ";
}
cout << '\n';
}