-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap_1.cpp
59 lines (44 loc) · 1.55 KB
/
hashmap_1.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
#include<bits/stdc++.h> //contains all the header files
/*
#include <iostream>
#include <unordered_map>
#include <string>
*/
using namespace std;
int main()
{
cout<<"-------------Program started-------------"<<endl;
//inbuilt unordered map ka use kr rhe hain so include kr liye hain
//ek unordered map ka object bana liye, key value pair me ek value string
//and ek value int
unordered_map<string,int> ourmap;
//now iske andr pair insert krne ke liye ek pair bana lenge
pair<string,int> p("abc",1);
//now ise insert kr lenge
ourmap.insert(p);
//second method of inserting
ourmap["def"]=2;
//means def key ke value me 2 daal diye
//find or access
cout<<ourmap["abc"]<<endl;
cout<<ourmap["def"]<<endl;
cout<<ourmap["ghi"]<<endl; //ghi koi key nhi hai phir bhi value 0 dikha rha hai
//so .at() function is safer to use
cout<<ourmap.at("ghi")<<endl;
//this is safe method kuki agar out of range access krenge to isme error show ho
//jayega bt upar wale me nhi show hoga jisse runtime pr garbage value milegi
//as because hm pehle hi ourmap["ghi"] run kr chuke so .at() method me bhi 0 show
//ho rha hai
//size
cout<<"size: "<<ourmap.size()<<endl;
//checking presence
//this fucntion tells whether somethings count 0 times or 1 times
//map me koi element ya to 0 baar ya 1 baar hi aa skti hai
if(ourmap.count("abc")>0){
cout<<"key :abc is present"<<endl;
}
//erase
ourmap.erase("ghi");
cout<<"size: "<<ourmap.size()<<endl;
return 0;
}