vector<pair<string,int>>排序

成绩排序

http://www.nowcoder.com/questionTerminal/8e400fd9905747e4acc2aeed7240978b

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
//#include <unordered_map>
using namespace std;

bool cmp0(const pair<string,int> &a,const pair<string,int> &b)
{
    return a.second>b.second;
}

bool cmp1(const pair<string,int> &a,const pair<string,int> &b)
{
    return a.second<b.second;
}

void mysort(int n,int flag)
{
    //unordered_map<string,int> res;
    vector<pair<string,int>> res(n);
    for(int i=0;i<n;i++)
    {
        cin>>res[i].first>>res[i].second;
    }
    if(flag==0)
    {
        stable_sort(res.begin(),res.end(),cmp0);
    }
    else if(flag==1)
    {
        stable_sort(res.begin(),res.end(),cmp1);
    }

    for(int i=0;i<n;i++)
    {
        cout<<res[i].first<<" "<<res[i].second<<endl;
    }
}

int main()
{
    int n,flag;
    while(cin>>n>>flag)
    {
        mysort(n,flag);
    }
    return 0;
}
全部评论

相关推荐

09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
9
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务