lucy2012 @ 2024-05-08 21:10:12
为什么我的代码又又又编译错误,biu的跳出一堆东西QwQ求调求调求调啊!
#include<bits/stdc++.h>
using namespace std;
int n;
struct stu{
string s;
int num,y,m,d;
}a[110];
int cmp(stu a,stu b){
if(a.y!=b.y)
return a.y<b.y;
if(a.m!=b.m)
return a.m<b.m;
if(a.d!=b.d)
return a.d<b.d;
return a.num<b.num;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s>>a[i].y>>a[i].m>>a[i].d;
a[i].num=i;
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++)
cout<<a[i].s<<endl;
return 0;
}
by zhangqiuyanAFOon2024 @ 2024-05-08 21:12:33
sort(a+1,a+1+n,cmp);
by GXZJQ @ 2024-05-08 21:13:02
@lucy2012 你好,很高兴能帮到你。
关于自定义排序函数返回值需要是 bool
型。
by NC20061226 @ 2024-05-08 21:13:08
@lucy2012
sort(a+1,a+1+n);
要加cmp,你这都没有注意到,应该改成
sort(a+1,a+1+n,cmp);
by CTGU_23_sxy @ 2024-05-08 21:13:40
sort这里加上cmp就好了 @lucy2012 ```cpp sort(a + 1, a + n + 1, cmp);
by CTGU_23_sxy @ 2024-05-08 21:14:01
sort(a + 1, a + n + 1, cmp);
by NC20061226 @ 2024-05-08 21:14:11
还有cmp要bool类型
by lucy2012 @ 2024-05-08 21:16:13
@NC20061226 @zhangqiuyan2023 好吧,是我有点逆天蒟蒻的基因在的QwQ学了几个月还是老犯这种问题怪不得我数学老是粗心错,谢谢啦!用的是bool但84分
by CTGU_23_sxy @ 2024-05-08 21:17:35
@lucy2012 int 可以的,cmp中是a.num > b.num
by CTGU_23_sxy @ 2024-05-08 21:19:10
不过最好还是写成bool
by lucy2012 @ 2024-05-08 21:19:10
@CTGU_23_sxy
找到了,谢谢啦!QwQ我眼睛聋了