Ordering a map by value
So I have a map of the form:
std::map<std::string, int> m;
I want to order this by its int value, but I don't know how to write such a function. I've had two attempts thus far:
One by using std::pair, as a map consists of pairs.
1 2 3
|
bool cmp(std::pair<std::string, int> p1, std::pair<std::string, int> p2) {
return p1.second < p2.second;
}
|
The other by trying to use maps, which seemed off to me at first, but I still tried it out (obviously it didn't work):
1 2 3
|
bool cmp(std::map<std::string, int> m1, std::map<std::string, int> m2) {
return m1.begin()->second < m2.begin()->second;
}
|
How do I write a function that compares the values of a map such that I can use it to sort the map?
You could use boost::multi_index.
Topic archived. No new replies allowed.