如何在多重地图中同时对键和值进行排序? [英] How to sort both key and value in a multimap?

查看:110
本文介绍了如何在多重地图中同时对键和值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

建议使用任何方法对键及其值的多重映射进行排序.例如- 输入- (5,1),(1,9),(1,1),(5,2),(1,2) 并且输出必须是- (1,1),(1,2),(1,9),(5,1),(5,2).

Suggest any method to sort a multimap for both key and its values. For example- Input- (5,1), (1,9), (1,1), (5,2), (1,2) And the Output must be- (1,1), (1,2), (1,9), (5,1), (5,2).

推荐答案

答案是emplace_hint.伪代码如下所示:-

The answer is emplace_hint. Pseudo code will look like that:-

insert_with_hint(M mmap, K key, V Value)
{
    auto i1 = mmap.equal_range(Key);
    for (auto i2 = i1.first; i2 != i1.second; ++i2)
    {
     if (i2->second > Key) { // <-- Here add your sorting criteria
           mmap.emplace_hint(i2,Key,Value)
      return
     }
   }
   mmap.emplace(Key,Value)
}

这篇关于如何在多重地图中同时对键和值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆