首页 > 编程语言 > 详细

C++Map按Value排序

时间:2018-09-19 21:29:26      阅读:328      评论:0      收藏:0      [点我收藏+]
#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排序

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!