|
| 1 | +#include<bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | +int main () |
| 4 | +{ |
| 5 | + map<int,string>m; |
| 6 | + // all the keys of map will always be unique |
| 7 | + // map stores keys & values in sorted order |
| 8 | + // if we keep string then it will be stored in lexographical order |
| 9 | + |
| 10 | + m[8]="Shivendra"; // insertion takes O(logn) |
| 11 | + m[3]="abc"; // O(logm) |
| 12 | + m[5]="cdc"; // when we wrote this even after there is no value it will take any value by it self |
| 13 | + m.insert({1,"shiv"}); |
| 14 | + /*map<int,string>:: iterator it; |
| 15 | + for (it=m.begin();it!=m.end();++it) |
| 16 | + { |
| 17 | + cout<<it->first<<" "<<it->second<<" "<<endl; // it will give the sorted output |
| 18 | + }*/ |
| 19 | + for (auto &pr :m){ |
| 20 | + cout<<pr.first<<" "<<pr.second<<" "<<endl; |
| 21 | + // this will also work as same like above commented code |
| 22 | + |
| 23 | + } |
| 24 | +} |
0 commit comments