Skip to content

Commit 82e9898

Browse files
authored
Sets in C++ STL (#369)
* Create Heap Sort in C++ * Codeforces solution of problem 49A * Create Sets in c++ STL
1 parent 4a0c428 commit 82e9898

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

CPP/stl/Sets in c++ STL

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// CPP program to demonstrate various functions of
2+
// Set in C++ STL
3+
#include <iostream>
4+
#include <iterator>
5+
#include <set>
6+
using namespace std;
7+
8+
int main()
9+
{
10+
// empty set container
11+
set<int, greater<int> > s1;
12+
13+
// insert elements in random order
14+
s1.insert(40);
15+
s1.insert(30);
16+
s1.insert(60);
17+
s1.insert(20);
18+
s1.insert(50);
19+
20+
// only one 50 will be added to the set
21+
s1.insert(50);
22+
s1.insert(10);
23+
24+
// printing set s1
25+
set<int, greater<int> >::iterator itr;
26+
cout << "\nThe set s1 is : \n";
27+
for (itr = s1.begin(); itr != s1.end(); itr++) {
28+
cout << *itr << " ";
29+
}
30+
cout << endl;
31+
32+
// assigning the elements from s1 to s2
33+
set<int> s2(s1.begin(), s1.end());
34+
35+
// print all elements of the set s2
36+
cout << "\nThe set s2 after assign from s1 is : \n";
37+
for (itr = s2.begin(); itr != s2.end(); itr++) {
38+
cout << *itr << " ";
39+
}
40+
cout << endl;
41+
42+
// remove all elements up to 30 in s2
43+
cout << "\ns2 after removal of elements less than 30 "
44+
":\n";
45+
s2.erase(s2.begin(), s2.find(30));
46+
for (itr = s2.begin(); itr != s2.end(); itr++) {
47+
cout << *itr << " ";
48+
}
49+
50+
// remove element with value 50 in s2
51+
int num;
52+
num = s2.erase(50);
53+
cout << "\ns2.erase(50) : ";
54+
cout << num << " removed\n";
55+
for (itr = s2.begin(); itr != s2.end(); itr++) {
56+
cout << *itr << " ";
57+
}
58+
59+
cout << endl;
60+
61+
// lower bound and upper bound for set s1
62+
cout << "s1.lower_bound(40) : \n"
63+
<< *s1.lower_bound(40) << endl;
64+
cout << "s1.upper_bound(40) : \n"
65+
<< *s1.upper_bound(40) << endl;
66+
67+
// lower bound and upper bound for set s2
68+
cout << "s2.lower_bound(40) :\n"
69+
<< *s2.lower_bound(40) << endl;
70+
cout << "s2.upper_bound(40) : \n"
71+
<< *s2.upper_bound(40) << endl;
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)