forked from jahid-hridoy/Code_Templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrdered_Set
More file actions
61 lines (50 loc) · 1.45 KB
/
Ordered_Set
File metadata and controls
61 lines (50 loc) · 1.45 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
// C++ program to demonstrate the
// ordered set in GNU C++
#include <iostream>
using namespace std;
// Header files, namespaces,
// macros as defined above
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
template <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//use less_equal for multiset
// Driver program to test above functions
int main()
{
// Ordered set declared with name o_set
ordered_set<int> o_set;
// insert function to insert in
// ordered set same as SET STL
o_set.insert(5);
o_set.insert(1);
o_set.insert(2);
// Finding the second smallest element
// in the set using * because
// find_by_order returns an iterator
cout << *(o_set.find_by_order(1))
<< endl;
// Finding the number of elements
// strictly less than k=4
cout << o_set.order_of_key(4)
<< endl;
// Finding the count of elements less
// than or equal to 4 i.e. strictly less
// than 5 if integers are present
cout << o_set.order_of_key(5)
<< endl;
// Deleting 2 from the set if it exists
if (o_set.find(2) != o_set.end())
o_set.erase(o_set.find(2));
// Now after deleting 2 from the set
// Finding the second smallest element in the set
cout << *(o_set.find_by_order(1))
<< endl;
// Finding the number of
// elements strictly less than k=4
cout << o_set.order_of_key(4)
<< endl;
return 0;
}