-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathmaps.cpp
More file actions
63 lines (49 loc) · 1.04 KB
/
maps.cpp
File metadata and controls
63 lines (49 loc) · 1.04 KB
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
//Every pair has uniqe key
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
map<string,int> m;
//1. Insert
m.insert(make_pair("Mango", 100));
//or
pair<string,int> p;
p.first = "Apple";
p.second = 120;
m.insert(p);
//or
m["banana"] = 20;
//2. Search
string fruit;
cout<<"Enter the fruit you want to search: "<<endl;
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;
}
m.erase(fruit);
//another way to find a particular map
if(m.count(fruit)){
cout<<"Price is "<<m[fruit]<<endl;
}
else{
cout<<"Could not find"<<endl;
}
m["lichi"] = 60;
m["pineapple"] = 80;
for(auto it=m.begin(); it!=m.end(); it++){
cout<<it->first<<" and "<<it->second<<endl;
}
cout<<endl;
//another method
for(auto p:m){
cout<<p.first<<" : "<<p.second<<endl;
}
return 0;
}