-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++ STL Set 8 (unordered set).cpp
85 lines (76 loc) · 1.57 KB
/
C++ STL Set 8 (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
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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
void insert(unordered_set<int> &s, int x);
void erase(unordered_set<int> &s, int x);
int size(unordered_set<int> &s);
int find(unordered_set<int> &s, int x);
int main()
{
// your code goes here
int t;
cin >> t;
while (t--)
{
unordered_set<int> s;
int q;
cin >> q;
while (q--)
{
char c;
cin >> c;
if (c == 'a')
{
int x;
cin >> x;
insert(s, x);
}
if (c == 'b')
{
int x;
cin >> x;
erase(s, x);
}
if (c == 'c')
{
int x;
cin >> x;
cout << find(s, x) << " ";
}
if (c == 'd')
cout << size(s) << " ";
}
cout << endl;
}
return 0;
}
// } Driver Code Ends
/* You are required to complete below methods */
/*inserts an element x to the unordered set s */
void insert(unordered_set<int> &s, int x)
{
s.insert(x);
}
/*erases an element x from the unordered set s */
void erase(unordered_set<int> &s, int x)
{
s.erase(x);
}
/*returns the size of the unordered set s */
int size(unordered_set<int> &s)
{
return s.size();
}
/*returns 1 if the element x is present
in unordered set s else returns -1 */
int find(unordered_set<int> &s, int x)
{
if (s.find(x) != s.end())
{
return 1;
}
else
{
return -1;
}
}