-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathunordered_set.cpp
42 lines (34 loc) · 984 Bytes
/
unordered_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
// Implemented using BST
// Modification of elements is allowed
#include <iostream>
#include <unordered_set>
using namespace std;
int main()
{
// declaration
unordered_set<int> ust;
// inserting elements takes O(1) time
ust.insert(1);
ust.insert(7);
ust.insert(8);
ust.insert(2);
ust.insert(5);
// printing the set
unordered_set<int> :: iterator itr;
for (itr = ust.begin(); itr != ust.end(); itr++)
{
cout << *itr << " ";
}
cout << endl;
// finding any elements require O(1)
// "find" function returns the iterator in O(1)
cout << "Print 5 if present ";
cout << *ust.find(5) << endl;
// "count" function returns bool value in O(1) time
cout << "Is 9 present in the set? " << ust.count(9) << endl;
// deleting all elements in the set
cout << "Is the set empty? " << ust.empty() << endl;
ust.clear();
cout << "Is the set empty now? " << ust.empty() << endl;
return 0;
}