map按照value排序的方法

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;
posted @ 2017-04-07 16:11  PyMCJ  阅读(323)  评论(0编辑  收藏  举报