forked from Gyanthakur/GFG_POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap_2.cpp
123 lines (100 loc) · 3.1 KB
/
hashmap_2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
// map in stl
// STL container which store key value pair
// the ele are store in accesnding or descending order
// maps cant have duplicate key
// implemented through BST;
int main(){
// map<string,int,greater<string>>m;
// m["gaurav"]=2134;
// m["ashish"]=34324;
// m["saurabh"]=4848;
// m["jayant"]=77228;
// m["ashish"]=77228;
// m.insert(make_pair("ballu",3234));
// m.insert({"dh",9879});
// for(auto ele:m){// data type of ele is key value pair
// cout<<"name - "<<ele.first<<endl;
// cout<<"no - "<<ele.second<<endl;
// }
// map<string,int>:: reverse_iterator itr;
// for(itr=m.rbegin();itr!=m.rend();itr++){
// cout<<itr->first<<endl;
// cout<<itr->second<<endl;
// }
// insert--O(logN);
// erase()
// size()
// max_size()
// find() if find then returns result else return end itr
// count();
// empty();
// upper_bound() -- returns next greater ele
// lower_bound()-- return ele if present else return next greater ele
// begin()
// end()
// rbegin()
// rend()
// Q---find the sum of repetitive ele in given arr
// 1 1 1 2 3 3 output 1+3=4
// time-O(N) space-O(n);
// int n;
// cin>>n;
// vector<int>v(n);
// for(int i=0;i<n;i++){
// cin>>v[i];
// }
// map<int, int>m ;
// for(int i=0;i<n;i++){
// m[v[i]]++;
// }
// int sum = 0;
// for(auto pair:m){
// if(pair.second>1){
// sum+=pair.first;
// }
// }
// cout<<sum<<endl;
// *******************unordered_map***********************
// ele are not ordered
// keys will be unique
// unordered_map<int,string>record;
// map<int,string>record;
// record.insert(make_pair(3,"ria"));
// record[1]="hans";
// record[2]="you";
// record[3]="divya";
// insert,erase,find all have O(1) time in avg case and O(n) in worst case
// count-if key present return 1 else 0;
// record.erase(3);
// record.erase(record.find(1));
// record.erase(record.find(1),record.end());
// for(auto pair:record){
// cout<<pair.first<<" "<<pair.second<<endl;
// }
// ***************multimap*****************
// ele are store in an order ascending or descending
// duplicate keys are allowed
// insert()
// delete()
// search() O(logn) time for all
// multimap<string,int>directory;
// directory.insert(make_pair("urvi",54322));
// directory.insert(make_pair("mohan",54322));
// directory.insert(make_pair("mohan",54322));
// for(auto pair:directory){
// cout<<pair.first<<" "<<pair.second<<endl;
// }
// **********unordered multimap*******
// insert,delete,search O(1) time for avg case and O(n) for worst case
// unordered_multimap<string,int>map;
// map.insert(make_pair("urvi",54322));
// map.insert(make_pair("mohan",54322));
// map.insert(make_pair("same",53322));
// for(auto pair:map){
// cout<<pair.first<<" "<<pair.second<<endl;
// }
return 0;
}