How to print a map backwards in reverse order in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <map>
 
void printMapBackwards(std::map<char, int> mp) {
   for (auto rit = mp.crbegin(); rit != mp.crend(); ++rit)
        std::cout << rit->first << ": " << rit->second << '\n';
}
 
int main ()
{
    std::map<char, int> mp;
 
    mp['a'] = 1;
    mp['x'] = 7;
    mp['r'] = 9;    
    mp['y'] = 4;
 
    printMapBackwards(mp);
    
    return 0;
}
 
 
 
/*
run:
 
y: 4
x: 7
r: 9
a: 1

*/

 



answered Apr 13, 2020 by avibootz

Related questions

1 answer 141 views
1 answer 187 views
1 answer 181 views
1 answer 114 views
1 answer 124 views
3 answers 230 views
...