蒟蒻对cmp函数的使用有些疑惑
已在代码块中标出
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef struct stu stu;
struct stu{
ll idx;
string name;
ll x,y,z;
};
bool cmp(stu ob1,stu ob2){
if(ob1.x==ob2.x&&ob1.y==ob2.y&&ob1.z==ob2.z)return 1//困惑的地方
else if(ob1.x==ob2.x&&ob1.y==ob2.y)return ob1.z<ob2.z;
else if(ob1.x==ob2.x)return ob1.y<ob2.y;
else return ob1.x<ob2.x;
}
void solve(){
ll n;cin>>n;
vector<stu>v;
for(ll i=0;i<n;++i){
string x;
ll y,z,e;cin>>x>>y>>z>>e;
v.push_back({i,x,y,z,e});
}
sort(v.begin(),v.end(),cmp);
for(auto &i:v)cout<<i.name<<"\n";
}
int main(){
ll T=1;
while(T--)solve();
return 0;
}
题目要求后输入的先输出,我以为cmp返回1就是不交换,也就是达到后输入的先输出的效果
然而#5测试点WA了,
后来将该处代码添加了顺序的比较,改为:
if(ob1.x==ob2.x&&ob1.y==ob2.y&&ob1.z==ob2.z)return ob1.idx>ob2.idx;
ac通过了.
请问各位dalao,cmp的含义到底是什么呢?