码迷,mamicode.com
首页 > 编程语言 > 详细

C++Map按Value排序

时间:2018-09-19 21:57:43      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:mes   name   include   algo   bool   string   opera   span   typedef   

#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

typedef pair<string,int> PAIR;

struct cmp
{
    bool operator()(const pair<string,int> &p1,const pair<string,int> &p2)
    {
        return p1.second > p2.second;
    }
};

int main()
{
    map<string,int>mp;

    for(int i=0;i<100;i+=10)
    {
        stringstream ss;
        ss<<i;
        string si = "m" + ss.str();
        mp.insert(make_pair(si,i));
    }

    for(map<string,int>::iterator it = mp.begin();it!=mp.end();it++)
    {
        cout<< it->first << "  " << it->second <<endl;
    }

    vector<pair<string,int>>vt(mp.begin(),mp.end());

    sort(vt.begin(),vt.end(),cmp());

    for(vector<pair<string,int>>::iterator it=vt.begin();it != vt.end();it++)
    {
        cout<< it->first << "  " <<it->second <<endl;
    }

    return 0;
}

 

C++Map按Value排序

标签:mes   name   include   algo   bool   string   opera   span   typedef   

原文地址:https://www.cnblogs.com/achao123456/p/9676524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!