nrk009

MAPSTL

Aug 2nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1.  
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int main() {
  6.     map<int,int>mp;
  7. //   map<int,int> m{ {1,2} , {2,3} , {3,4} }
  8.     //   m.insert( pair<int,int> (4,5));
  9.       //  map <char ,int > mp;
  10.       //  mp[‘b’]  = 1;
  11.     int n;
  12.     cin>>n;
  13.     //INITIALISE MAP
  14.     for(int i=0;i<n;i++)
  15.     { int a,b;
  16.         cin>>a>>b;
  17.        // mp.insert(make_pair(a,b));
  18.         mp.insert({a,b});
  19.         //mp[a[i]]++;
  20.        // mp[i]=make_pair(a,b); cannot do this,wrong way of initialisation
  21.     }
  22.     //OUTPUT MAP;
  23.     map<int,int>::iterator it,it1;
  24.     for(it=mp.begin();it!=mp.end();it++)
  25.     cout<<it->first<<" "<<it->second<<" ";
  26.  
  27.         for(auto it=mp.begin();it!=mp.end();it++)
  28.     cout<<it->first<<" "<<it->second<<" ";
  29.  
  30.     for(auto it:mp)
  31.     cout<<it.first<<" "<<it.second<<" ";
  32.     //MAP IN REVERSE ORDER
  33.     map<int,int>::reverse_iterator itr;
  34.     for(itr=mp.rbegin();itr!=mp.rend();itr++)
  35.                 cout<<itr->first<<" "<<itr->second << ;
  36.  
  37.         for(auto itr=v.rbegin();itr!=v.rend();itr++)
  38.                 cout<<itr->first<<" " << itr -> second;
  39.  
  40.     // ERASE FUNCTION
  41.     mp.erase(it) //erase element at iterator position ;
  42.     mp.erase (4);    // remove all elements with key = 4;
  43.     mp.erase(mp.begin(), mp.find(3); //
  44.         mp.erase(it1,it2) //erases element in a range;
  45.  
  46.         mp.clear(); //clear the map;
  47.         //SORT IN MAP
  48.  
  49.          // Here if greater<int> is used to make
  50.     // sure that elements are stored in
  51.     // ascending order of keys.
  52.        map<int, string, greater <int> > mymap;
  53.  
  54.          //values in map are already sort according to their key;
  55.          //if we want to sort it by value then store in a different container;
  56.  
  57.           map<char,int> map2 (map1.begin(), map1.end());
  58.      /* creates a map map2 which have entries copied
  59.         from map1.begin() to map1.end() */
  60.  
  61.          map<char,int> map3 (m);
  62.       /* creates map map3 which is a copy of map m */
  63.  
  64.       mp.empty();
  65. }
  66.     return 0;
  67. }
Add Comment
Please, Sign In to add comment