|
| 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(nlogn) |
| 10 | + cout<<pr.first<<" "<<pr.second<<" "<<endl; |
| 11 | +} |
| 12 | + |
| 13 | +int main () |
| 14 | +{ |
| 15 | + map<int,string>m; |
| 16 | + // all the keys of map will always be unique |
| 17 | + // map stores keys & values in sorted order |
| 18 | + // if we keep string then it will be stored in lexographical order |
| 19 | + |
| 20 | + m[8]="Shivendra"; // insertion takes O(logn) |
| 21 | + m[3]="abc"; // O(logm) |
| 22 | + m[5]="cdc"; // when we wrote this even after there is no value it will take any value by it self |
| 23 | + m.insert({1,"shiv"}); |
| 24 | + print(m); |
| 25 | + |
| 26 | + } |
| 27 | +} |
0 commit comments