File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
// Lets make a function to print map and getting its size
5+
6+
void print(map<int,string> &m)
7+
{
8+
cout<<"Size of the map is "<<m.size()<<endl;
9+
for (auto pr :m) // this loop is of O(n)
10+
cout<<pr.first<<" "<<pr.second<<" "<<endl; // O(nlogn)
11+
}
12+
// map uses red black self balencing tree in backend and it always stored keys in sorted manner
13+
int main ()
14+
{
15+
map<int,string>m;
16+
// map can store any kind of data types even set, vector ..
17+
// all the keys of map will always be unique
18+
// map stores keys & values in sorted order
19+
// if we keep string then it will be stored in lexographical order
20+
21+
m[8]="Shivendra"; // insertion takes O(logn)
22+
m[3]="abc"; // O(logm)
23+
m[5]="cdc"; // when we wrote this even after there is no value it will take any value by it self
24+
m.insert({1,"shiv"});
25+
// when you want to directly access some specific value in map using m[key] or m.find(key), these are log(n) operations as log(n)time is taken by map to search this key.
26+
m.erase(3); // O(log(n))
27+
auto it = m.find(3); // O(long(n))
28+
m.erase(it); // O(log(n)) // it will delete the key 3 and value
29+
m.clear(); // it will clear(delete) full map
30+
if (it== m.end())
31+
cout<<"NO Value ";
32+
else
33+
cout <<it->first <<" "<<it->second <<" "<<endl;
34+
/*print(m);*/
35+
print(m);
36+
}
37+
//

0 commit comments

Comments
 (0)