Get The Top 10 Values of a Map C++ [duplicate]
Possible Duplicate:
Sorting a std::map by value before output & destroy
map<string, int> Hosts;
Which contain a host and the amount of times that host was accessed.
How can I get the top X v开发者_运维百科alues of that map?
I think if you use std::vector<std::pair<std::string, int>
and use std::sort
providing your own compare
function (or functor), that would solve this problem more easily. Also you use std::map
which sorts the elements by their key. I think you don't need container which sorts by key (string
), but by value (int
).
EDIT: I just noticed that even the possible duplicate does the same thing as I said. It uses std::vector
, so you also use this as:
std::vector<std::pair<std::string, int> Hosts; //Use this instead of map!
See comments for answer. Sorting a std::map by value before output & destroy
After some modification (changing the for to only go to map.size()) it works perfect. Could people please vote to delete?
精彩评论