-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmaps.cpp
50 lines (41 loc) · 1.05 KB
/
hashmaps.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
#include<iostream>
#include<map>
#include<unordered_map>
using namespace std;
int main(){
// Creation
// unordered_map<string , int> m;
map<string , int> m;
// Insertion
pair<string , int> p = make_pair("babbar" , 3);
m.insert(p);
pair<string , int> pair2("love" , 2);
m.insert(pair2);
m["mera"] = 1;
m["mera"] = 2;
// Search
cout<<m["mera"]<<endl;
cout<<m.at("babbar")<<endl;
cout<<m["unknownkey"]<<endl;
cout<<m.at("unknownkey")<<endl;
// Size
cout<<m.size()<<endl;
// to check presence
cout<<m.count("love")<<endl;
// erase
m.erase("love");
cout<<m.size()<<endl;
for(auto i:m){
cout<<i.first<<" "<<i.second<<endl;
}
// iterator
// unordered_map<string , int> :: iterator it = m.begin();
map<string , int> :: iterator it = m.begin();
while(it != m.end()){
cout<<it->first<<" "<<it->second<<endl;
it++;
}
// Time Complexity of map is O(log n).
// Time Complexity of unordered_map is O(1).
return 0;
}