map按照value排序的方法

本文详细介绍如何将C++中的map以pair(string,double)形式存储并转换至vector中,同时展示了如何通过自定义比较器对vector进行排序。通过具体实例,深入理解C++ STL容器的使用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.将map以pair(string,double)的存储方式加入vector中

2.vector的sort()中的第三个参数重载

#typedef pair<string,double>  PAIR

struct CmpByValue {
bool operator()(const PAIR& lhs, const PAIR& rhs) {
    return lhs.second < rhs.second;
  }
};

 

    map<QString, int> name_score_map;
      name_score_map["LiMin"] = 90;
      name_score_map["ZiLinMi"] = 79;
      name_score_map["BoB"] = 92;
      name_score_map.insert(make_pair("Bing",99));
      name_score_map.insert(make_pair("Albert",86));
     //把map中元素转存到vector中

      vector<PAIR> name_score_vec(name_score_map.begin(), name_score_map.end());
      sort(name_score_vec.begin(), name_score_vec.end(), CmpByValue());
     // sort(name_score_vec.begin(), name_score_vec.end(), cmp_by_value);

      for (int i = 0; i != name_score_vec.size(); ++i) {

          qDebug()<<name_score_vec[i];
      }
      qDebug()<<name_score_vec[name_score_vec.size()-1].first;

转载于:https://www.cnblogs.com/Pymcj/p/6678650.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值