-
Notifications
You must be signed in to change notification settings - Fork 17
/
unordered map implementation.cpp
83 lines (68 loc) · 2.04 KB
/
unordered map implementation.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
//internally implemented using hash map
//all the code is same as that of a map
//the only difference is that we'll not get the element in sorted ordered
#include <iostream>
#include<unordered_map>
#include<string>
using namespace std;
int main()
{
//initializing a map
unordered_map<string,int>m;
//insetring into map elements
m.insert(make_pair("Mango",1000));
//inserting M-02
pair<string,int>p;
p.first="Apple";
p.second=1200;
m.insert(p);
//inserting M-03
m["Banana"]=2000;
//searching in the map M-01
string fruit;
cout<<"enter any fruit:";
cin>>fruit;
auto it = m.find(fruit);
if(it!=m.end())
cout<<"\nprice is: "<<fruit<<m[fruit]<<"\n";
else
cout<<"not found\n";
//searching M-02
//{it store unique key only once}
if(m.count(fruit))
cout<<"price is: "<<m[fruit]<<endl;
else
cout<<"not found!!\n";
//erase a particular key
m.erase(fruit);
//update the key value
m[fruit]+=2903;
//iterate over all key values
m["Lichi"]=60;
m["Pineapple"]=100;
for(auto it=m.begin();it!=m.end();it++)
{
cout<<it->first<<":"<<it->second<<"\n";
}
//for each loop
for(auto p:m)
cout<<p.first<<"<=>"<<p.second<<endl;
return 0;
}
/*
output:
enter any fruit:not found
not found!!
Mango:1000
Pineapple:100
Lichi:60
Banana:2000
Apple:1200
:2903
Mango<=>1000
Pineapple<=>100
Lichi<=>60
Banana<=>2000
Apple<=>1200
<=>2903
*/