相关题目:
三元组排序https://www.acwing.com/problem/content/864/https://www.acwing.com/problem/content/864/
相关代码:
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
struct stc {
int x;
double y;
string z;
};
bool cmp(stc x,stc y){
return x.x<y.x;
}
int main(){
int N;
cin>>N;
struct stc st[N];
for(int i=0;i<N;i++){
cin>>st[i].x;
cin>>st[i].y;
cin>>st[i].z;
}
sort(st,st+N,cmp);
for(int i=0;i<N;i++){
printf("%d %.2lf %s\n",st[i].x,st[i].y,st[i].z.c_str());
}
return 0;
}
补充:
1. sort(a,a+n,cmp),该方法存于库文件#include<algorithm>,cmp表示的时自定义排序函数,可以定义顺序或者逆序。
bool cmp(int x,int y){
return x<y;
}
2. printf("%s",s.c_str());