C++,代码美观,STL的稳定排序和用Lambda写小函数

成绩排序

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

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int n,flag;
    while(cin>>n>>flag)
    {
        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(),[](const pair<string,int> &a,const pair<string,int> &b){
                return a.second>b.second;
            });
        }
        else if(flag==1)
        {
            stable_sort(res.begin(),res.end(),[](const pair<string,int> &a,const pair<string,int> &b){
                return a.second<b.second;
            });
        }
        for(int i=0;i<n;i++)
            cout<<res.at(i).first<<" "<<res.at(i).second<<endl;
    }
    return 0;
}
全部评论
调库侠,机考的时候别哭
点赞 回复 分享
发布于 2023-04-05 00:49 香港
stable_sort({})这种格式的函数还第一次见 有大佬解释一下吗
点赞 回复 分享
发布于 2022-05-08 15:00
谢谢
点赞 回复 分享
发布于 2022-04-14 12:04
用系统函数不算作弊吗
点赞 回复 分享
发布于 2022-01-25 09:12

相关推荐

鼠鼠第一次实习,啥也不懂一直是自己一个人吃的饭,不会做工作老是被嫌弃,大人的世界是这样的吗?
我是星星我会发亮:好的mt有两种,一种愿意教你的,一种几乎什么活都不给你派让你很闲允许你做自己事情的
点赞 评论 收藏
分享
牛客刘北:如果暑期实习是27届的话,你要晚一年才会毕业,企业为什么会等你呢?要搞清时间逻辑呀!27届现在实习只能是在暑假实习,这是日常实习,不是暑期实习。所以多去投日常实习吧,暑期实习肯定不会要你的
点赞 评论 收藏
分享
评论
38
7
分享

创作者周榜

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