简体   繁体   English

std :: map按值大小排序(设置 <int> )

[英]std::map sort by size of value (set<int>)

Using C++, I have a map with an int key and a set value. 使用C ++,我有一个带有int键和设置值的映射。 I'd like to sort the map for output based on the size of the set (ie, value.size()). 我想根据集合的大小(即value.size())对地图进行排序以输出。 I know the map automatically sorts itself based on the key. 我知道地图会根据密钥自动对自身进行排序。 Is there some way to make the map sort by number of elements in its value? 有什么方法可以使地图按值中的元素数量排序? Also, if I flip the map, as some posts suggest, and the key becomes set, how does the map determine the sort order? 另外,如果像某些帖子所建议的那样翻转地图,并且键已设置,则地图如何确定排序顺序? Any help understanding this would be much appreciated. 任何帮助理解这一点将不胜感激。

mymap<int, set<int> >;
/* code to fill map */
/* How to sort by mymap[node].second().size() ??? */

You can't change the sort order of an existing map . 您无法更改现有map的排序顺序。

Illustrating one way to create an index into the map in the form of a sorted vector ... 说明一种以排序vector的形式在map中创建索引的方法...

std::vector<std::pair<int, int>> size_key;

for (auto& x : mymap)
    size_key.emplace_back(x.second.size(), x.first);
std::sort(std::begin(size_index), std::end(size_index));

// work's done above - just display the results to illustrate access...

for (auto& sk : size_key)
{
    std::cout << "with size " << sk.first
              << ", value " << mymap[sk.second].first << '\n';
    for (auto& n : mymap[sk.second].second)
         std::cout << "  " << n << '\n'
}

Also, if I flip the map, as some posts suggest... 另外,如果我翻转地图,正如一些帖子所建议的那样...

Not a good idea: just "flipping" key and value won't help without also changing the sort comparitor, you'd need a multimap if set sizes could be repeated, and slower and more wasteful of memory than building an index as above (unless you can throw away the original mymap once the re-ordered multimap is constructed). 这不是一个好主意:只是“翻转”键和值不会不还更改排序比较器的帮助,你需要一个multimap ,如果set的大小可以重复,而且速度较慢,比建立索引如上更浪费内存(除非在重新排序的multimap构建后可以丢弃原始的mymap )。

You just want to output the map in some weird sorted order. 您只想以某种奇怪的排序顺序输出地图。

You should not want to modify the order of keys inside a std::map (that does not make any sense, since the fixed order -ie the key compare function - is the second template parameter of std::map ). 您不希望修改std::map内部的键顺序(这没有任何意义,因为固定顺序(即键比较功能)是std::map的第二个模板参数)。

So get the std::vector of keys from the std::map : 因此,从std :: map获取键的std :: vector

std::vector<int> keyvec;
keyvec.reserve(mymap.size());
for (auto&x : mymap)
  keyvec.push_back(x.first);

then sort (using std::stable_sort to keep keys of same size in original order) it according to your criteria 然后根据您的条件进行排序(使用std :: stable_sort保持相同大小的键保持原始顺序)

std::stable_sort(keyvec.begin(), keyvec.end(),
                 [&mymap,=](int k1, int k2) 
                 { return mymap[k1].size() < mymap[k2].size(); });

and at last display your map using 并利用最后显示地图

for (auto k: keyvec)
   std::cout << "[" << k << "]:" << mymap[k] << std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM