dimanche 3 mai 2020

Sorting of STL map in c++ based on value

I've a follwing map:

map<int, int> mp;
mp[11] = 5;
mp[3] = 7;
mp[1] = 3;

If I iterate over map like:

for(auto it: mp) {
    cout<<it.first<<" "<<it.second<<'\n';
}

Then the output will be:

1 3
3 7
11 5

But I want which key has the greater value that key will appear first. Example output for the case:

3 7
11 5
1 3

How can I do this?

Aucun commentaire:

Enregistrer un commentaire