-
Notifications
You must be signed in to change notification settings - Fork 1
/
unordered_map stl.cpp
67 lines (47 loc) · 1.1 KB
/
unordered_map stl.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
#include <iostream>
#include<unordered_map>
#include<string>
using namespace std;
int main() {
unordered_map<string,int> m;
//1. Insert
m.insert(make_pair("Mango",100));
pair<string,int> p;
p.first = "Apple";
p.second = 120;
m.insert(p);
m["Banana"] = 20;
//2. Search
string fruit;
cin>>fruit;
//update the price
m[fruit] += 22;
auto it = m.find(fruit);
if(it!=m.end()){
cout<<"price of "<<fruit<<" is"<<m[fruit]<<endl;
}
else{
cout<<"fruit is not present "<<endl;
}
//3. Erase
m.erase(fruit);
//another way to find a particular map
// it stores unique keys only once
if(m.count(fruit)){
cout<<"Price is "<<m[fruit]<<endl;
}
else{
cout<<"Couldnt find "<<endl;
}
m["Litchi"] = 60;
m["Pineapple"] = 80;
//Iterate over all the key value pairs
for(auto it=m.begin();it!=m.end();it++){
cout<<it->first<<" and "<<it->second<<endl;
}
//for each loop
for(auto p:m){
cout<<p.first<<" : "<<p.second<<endl;
}
return 0;
}