Reputation: 2718
I searched a lot but I didnt find the answer I want.
I need to sort c++ map based on keys and values. something like this method :
bool mycomp(mii::iterator a, mii::iterator b) {
if (a->second > b->second)
return true;
else if (a->second < b->second)
return false;
else
return a->first > a->second;
}
and use it something like that
sort(m.begin() , m.end(), mycomp);
where m is :
map<int,int> m;
can I do such thing ? if yes, what should be the correct syntax.
Upvotes: 0
Views: 3075
Reputation: 1320
When you define the std::map
you can provide a Compare
function as a template parameter. See the reference for more details.
However, I believe this is only for key sorting. Key-value sorting is not inherit to the map structure (i.e. what happens if the value changes?)
Upvotes: 1