Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int main() {
- map<int,int>mp;
- // map<int,int> m{ {1,2} , {2,3} , {3,4} }
- // m.insert( pair<int,int> (4,5));
- // map <char ,int > mp;
- // mp[‘b’] = 1;
- int n;
- cin>>n;
- //INITIALISE MAP
- for(int i=0;i<n;i++)
- { int a,b;
- cin>>a>>b;
- // mp.insert(make_pair(a,b));
- mp.insert({a,b});
- //mp[a[i]]++;
- // mp[i]=make_pair(a,b); cannot do this,wrong way of initialisation
- }
- //OUTPUT MAP;
- map<int,int>::iterator it,it1;
- for(it=mp.begin();it!=mp.end();it++)
- cout<<it->first<<" "<<it->second<<" ";
- for(auto it=mp.begin();it!=mp.end();it++)
- cout<<it->first<<" "<<it->second<<" ";
- for(auto it:mp)
- cout<<it.first<<" "<<it.second<<" ";
- //MAP IN REVERSE ORDER
- map<int,int>::reverse_iterator itr;
- for(itr=mp.rbegin();itr!=mp.rend();itr++)
- cout<<itr->first<<" "<<itr->second << ;
- for(auto itr=v.rbegin();itr!=v.rend();itr++)
- cout<<itr->first<<" " << itr -> second;
- // ERASE FUNCTION
- mp.erase(it) //erase element at iterator position ;
- mp.erase (4); // remove all elements with key = 4;
- mp.erase(mp.begin(), mp.find(3); //
- mp.erase(it1,it2) //erases element in a range;
- mp.clear(); //clear the map;
- //SORT IN MAP
- // Here if greater<int> is used to make
- // sure that elements are stored in
- // ascending order of keys.
- map<int, string, greater <int> > mymap;
- //values in map are already sort according to their key;
- //if we want to sort it by value then store in a different container;
- map<char,int> map2 (map1.begin(), map1.end());
- /* creates a map map2 which have entries copied
- from map1.begin() to map1.end() */
- map<char,int> map3 (m);
- /* creates map map3 which is a copy of map m */
- mp.empty();
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment