大牛们想问个关于cmp的问题
查看原帖
大牛们想问个关于cmp的问题
1149836
thesadbee楼主2024/3/24 20:11

以“2 7 72”为输入,我这个cmp函数为什么写成“return (a.name+b.name)>(b.name+a.name);”就能正确返回“772”,写成下面这样就会错误返回“727”啊 ……我感觉是我对cmp内部判定的理解出了问题,希望各位大牛帮帮

#include <bits/stdc++.h>
using namespace std;

struct student
{
  string name;
}stu[21];

bool cmp(student a,student b){
 if((a.name+b.name)>(b.name+a.name)) return a.name>b.name;
  }

int main(){
int a;
scanf("%d",&a);
for(int i=0;i<a;i++){
  string p;
  cin>>p;
  stu[i].name=p;
}
sort(stu,stu+a,cmp);
for(int i=0;i<a;i++){
  cout<<stu[i].name;
}
}


2024/3/24 20:11
加载中...