我试图打印出五个最常用的值。但是,当我将地图更改为多地图时,我打破了向地图添加值的代码。我如何将值添加到多图?它可以以类似的方式完成,因为我将值添加到地图中?将值添加到std :: multimap
// Create a map for keeping track of how many occurences of the different colors
multimap<string, int> hexmap;
// Add the hex values to the map
for(int i = 0; i < imagesize; i ++)
{
hexmap[colors[i]]++;
}
typedef std::multimap<int, string> Mymap;
Mymap dst;
std::transform(hexmap.begin(), hexmap.end(),
std::inserter(dst, dst.begin()),
[](const std::pair<string,int> &p)
{
return std::pair<int, string>(p.second, p.first);
}
);
Mymap::iterator st = dst.begin(),it;
size_t count = 5;
for(it = st; (it != dst.end()) && (--count); ++it)
std::cout << it->second << it->first << endl;
您可能感兴趣的Boost.Bimap。 –
multimaps不支持通过operator []'进行访问,所以您的'hexmap [colors [i]] ++'行无效。你有把'hexmap'变成多图的原因吗?它似乎可以保留为常规地图。 – Alec
@alecbenzer是的,我想从hexmap recive五个最高值,并有不止一个具有相同的映射值。 – user2520739