<分区>
Possible Duplicate:
Sorting a std::map by value before output & destroy
map<string, int> Hosts;
其中包含一个主机和该主机被访问的次数。
如何获取该 map 的前 X 值?
标签 c++
<分区>
Possible Duplicate:
Sorting a std::map by value before output & destroy
map<string, int> Hosts;
其中包含一个主机和该主机被访问的次数。
如何获取该 map 的前 X 值?
最佳答案
我想如果你使用 std::vector<std::pair<std::string, int>
并使用 std::sort
提供你自己的compare
函数(或仿函数),这将更容易地解决这个问题。你也用 std::map
它按键对元素进行排序。我认为您不需要按键(string
)排序的容器,而是按值(int
)排序的容器。
编辑:我刚刚注意到,即使是可能的重复项也和我说的一样。它使用 std::vector
, 所以您也可以将其用作:
std::vector<std::pair<std::string, int> Hosts; //Use this instead of map!
关于c++ - 获取 map C++ 的前 10 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5360367/