-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_STL.cpp
83 lines (70 loc) · 1.67 KB
/
set_STL.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
#include<iostream>
#include<set>
using namespace std ;
//Balanced BST
//Sorted element
//Random access not possible
//Multiset allows dublication of elements
//multiset<int> s ;
//On using s.erase(any element that occurs multitimes)
//will result in erasing all occurance of that element
//for deletion of first occurance of any element
//s.erase(s.find(3));
// - Insertion : O(log(n))
// - Deletion : O(log(n))
// - Lower / Upper_bound : O(log(n))
// - Printing : O(nlogn)
//Unordered Set
//Implemented using Hashing
//Not in sorted order
//#include<unordered_set>
// - Insertion : O(1) in avg case , O(n) in worst case
// - Deletion : O(1) in avg case , O(n) in worst case
int main()
{
set<int> s ;
s.insert(1);
s.insert(2);
s.insert(2);
s.insert(3);
//printing the elements in the set
//using foreach loop
for(auto i : s)
cout << i <<" ";
cout << "\n";
for (auto i = s.begin(); i!= s.end(); i++)
{
cout<< *i << " " ;
}
cout << "\n";
//print in reverse
for(auto i = s.rbegin() ; i != s.rend() ; i++)
{
cout << *i <<" ";
}
cout << "\n";
//size of set
cout << s.size();
cout << "\n";
//Deleting all elements of set ;
s.clear();
s.insert(1);
s.insert(3);
s.insert(5);
cout << *s.lower_bound(2) << "\n";
cout << *s.lower_bound(3) << "\n";
cout << *s.upper_bound(3) << "\n";
cout << *s.upper_bound(5) << "\n";
//declaring set in decending order
set<int , greater<int>> ss;
ss.insert(1);
ss.insert(2);
ss.insert(2);
ss.insert(3);
for(auto i : ss)
{
cout << i << " " ;
}
cout << "\n";
s.clear();
}