stable_sort和sort的区别

acwing4711.排队

假设oneoke按某种排序方式,他们是相等的,如果使用的sort排序,在给他们排序后oke可能会出现在one的后面,而你使用stable_sort排序,你会发现他们的次序一定是不变的,保留原有的顺序。

#include <iostream>
#include <cstring>
#include <unordered_map>
#include <algorithm>
using namespace std;

int n;
struct node{
  string name,identity;
  int p;
}node[10001];
bool cmp(const struct node& n1,const struct node& n2){
    return n1.p<n2.p;
}
int main()
{
    cin>>n;
    unordered_map<string,int> umap;
    umap["rat"]=1;
    umap["woman"]=umap["child"]=2;
    umap["man"]=3;
    umap["captain"]=4;
    for(int i=0;i<n;i++){
        string a,b;
        cin>>a>>b;
        node[i].name=a;
        node[i].identity=b;
        node[i].p=umap[b];
    }
    stable_sort(node,node+n,cmp);
    for(int i=0;i<n;i++){
        cout<<node[i].name<<endl;
    }
    
    
    
}

这里给出答案,大家可以试试sort,有些测试会过不去的!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值