-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathunordered_map.cpp
80 lines (61 loc) · 1.51 KB
/
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
#include <iostream>
#include <unordered_map>
using namespace std;
/*
map is based on red-black tree
unordered map is based on hashing
map stores keys in ordered form
in unordered map there is no order of keys
with map serach, insert, delete is O(logn)
with unordred map, it is O(1)
[] is member access operator in maps
syntax: m[key] = value
if key present the [] operator gives reference for the value
else if not present, it inserts the key
Time complexity:
insert(), erase(key), count(), find(), [], at, is O(1) on average
*/
void example1()
{
unordered_map<string, int> m;
// defining a key gfg and assigning a value of 20
m["gfg"] = 20;
m["ide"] = 30;
// another of inserting a key-value pair
m.insert({"courses", 15});
for(auto x: m)
cout << x.first << " " << x.second << "\n";
/*
op:
gfg 20
courses 15
ide 30
Notice the random order in which pairs were inserted
*/
}
void isPresentExample()
{
unordered_map<string, int> m;
m["gfg"] = 20;
m["ide"] = 30;
m["courses"] = 15;
if(m.find("ide") != m.end())
cout << "\nFound";
else
cout << "\nNot found";
// or
if(m.count("ide") > 0)
cout << "Found";
else
cout << "Not found";
for (auto it = m.begin(); it != m.end(); it++)
cout << it->first << " " << it->second << "\n";
// m.size() returns no of key value pair
// m.erase("ide") removes key value pair
}
int main()
{
example1();
isPresentExample();
return 0;
}