-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++ STL Set 7 (unordered map).cpp
87 lines (77 loc) · 1.69 KB
/
C++ STL Set 7 (unordered map).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
86
87
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
void add_value(unordered_map<int, int> &m, int x, int y);
int find_value(unordered_map<int, int> &m, int x);
int getSize(unordered_map<int, int> &m);
void removeKey(unordered_map<int, int> &m, int x);
int main()
{
// your code goes here
int t;
cin >> t;
while (t--)
{
unordered_map<int, int> m;
int q;
cin >> q;
while (q--)
{
char c;
cin >> c;
if (c == 'a')
{
int x, y;
cin >> x >> y;
add_value(m, x, y);
}
if (c == 'b')
{
int y;
cin >> y;
cout << find_value(m, y) << " ";
}
if (c == 'c')
{
cout << getSize(m) << " ";
}
if (c == 'd')
{
int x;
cin >> x;
removeKey(m, x);
}
}
cout << endl;
}
return 0;
}
// } Driver Code Ends
/*You are required to complete below methods */
/*Inserts an entry with key x and value y in map */
void add_value(unordered_map<int, int> &m, int x, int y)
{
m[x] = y;
}
/*Returns the value with key x from the map */
int find_value(unordered_map<int, int> &m, int x)
{
if (m.find(x) != m.end())
{
return m[x];
}
return -1;
}
/*Returns the size of the map */
int getSize(unordered_map<int, int> &m)
{
return m.size();
}
/*Removes the entry with key x from the map */
void removeKey(unordered_map<int, int> &m, int x)
{
if (m.find(x) != m.end())
{
m.erase(x);
}
}