-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStl2_hashmap.cpp
More file actions
58 lines (43 loc) · 893 Bytes
/
Stl2_hashmap.cpp
File metadata and controls
58 lines (43 loc) · 893 Bytes
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
#include<iostream>
#include<unordered_map>
using namespace std;
class Fruit{
public:
string name;
int price;
string city;
Fruit()
{
}
Fruit(string n,int p,string c)
{
name=n;
price = p;
city = c;
}
};
int main()
{
unordered_map<string,Fruit> h;
Fruit f("Mango",100,"Delhi");
h[f.name]= f;
Fruit fs = h["Mango"];
cout<<fs.city<<endl;
cout<<fs.price<<endl;
///Count function return - 0 or 1
cout<<"Apple "<<h.count("Apple")<<endl;
cout<<"Mango "<<h.count("Mango")<<endl;
cout<<"Enter a Fruit to search ";
string fruit;
cin>>fruit;
h.erase("Mango");
if(h.count(fruit)!=0)
{
cout<<"price is "<<h[fruit].price<<endl;
}
else
{
cout<<"Fruit doesn't exist";
}
return 0;
}