mercredi 23 octobre 2019

How to iterate map::crbegin() without using auto keyword

I have made a map and I want to iterate it in reverse order. I know that i can do it by using auto keyword like this

#include <bits/stdc++.h>
using namespace std;
int main()
{ 
   map <int,int> mp;

   mp.insert(make_pair(3,30));
   mp.insert(make_pair(4,90));
   mp.insert(make_pair(2,130));
   mp.insert(make_pair(1,20));
   mp.insert(make_pair(5,10)); 

   auto it = mp.crbegin();
   while(it!=mp.crend())
   {
    cout<<it->first <<" "<< it->second <<endl;
     it++;
   }  
}

What can i use instead of auto keyword ?

My below code gives me compilation error.

#include <bits/stdc++.h>
using namespace std;
int main()
{ 
  map <int,int> mp;
  mp.insert(make_pair(3,30));
  mp.insert(make_pair(4,90));
  mp.insert(make_pair(2,130));
  mp.insert(make_pair(1,20));
  mp.insert(make_pair(5,10)); 

  map<int,int>::iterator it = mp.crbegin();
  while(it!=mp.crend())
  {
    cout<<it->first <<" "<< it->second <<endl;
    it++;
  }  
}

Is it possible or not?

Aucun commentaire:

Enregistrer un commentaire