日志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; }