wolfrevoda 2014-03-07 02:36 采纳率: 0%
浏览 1642

一个有关sort函数第三个参数返回值的疑问

#include
#include
#include

using namespace std;

const int maxsize = 1000;

struct stu{
char name[100];
int age;
int score;
};

bool cmp(stu a,stu b)
{
if(a.score < b.score)
return true;
int temp = strcmp(a.name,b.name);
if(temp < 0)
return true;
if(a.age < b.age)
return true;
return false;
}

int main()
{
int n;
struct stu s[maxsize];
while(scanf("%d",&n) != EOF)
{
for(int i = 0;i < n;++i)
{
scanf("%s",&s[i].name);
scanf("%d",&s[i].age);
scanf("%d",&s[i].score);
}
sort(s,s+n,cmp);
for(int i = 0;i < n;++i)
{
printf("%s ",s[i].name);
printf("%d ",s[i].age);
printf("%d",s[i].score);
printf("\n");
}
}

return 0;
}
在这里的cmp函数,在运行时总是弹出invalid operator < 的错误,但是如果将cmp中的return true换成return a.score < b.score之类的就对了,这是为什么了?函数sort中第三个参数需要返回的不就是bool值吗?

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-20 17:59
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示