日志10

结构体排序

对于结构体排序其实也不难,只是需要我们自己重写cmp函数
例如要对结构体中的元素b按照升序排序。

#include<bits/stdc++.h>  using namespace std; struct node{  int a; int b;
};  bool cmp(node time1,node time2) { return time1.b<time2.b;
} int main() {  int n;
    cin>>n;
    node time[n+10]; for(int i=0;i<n;i++)
        cin>>time[i].a>>time[i].b; sort(time,time+n,cmp); for(int i=0;i<n;i++)
    {
        cout<<time[i].a<<" "<<time[i].b<<endl;
    } return 0;
}

全部评论

相关推荐

我的名字是句号:接好运
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务