Let me talk about the reason first: 自定义的比较函数不能带等号,只能小于或大于
a class is defined as follows:
class Flow {
public:
int id;
int bandwidth;
int arrTime;
int startTime;
int needTime;
Flow(int i, int b, int a, int n);
};
A comparison function is defined as follows:
bool sortFlowsByArrtime(Flow x, Flow y) {
return x.arrTime <= y.arrTime;
}
Then output after sorting:
sort(flows.begin(),flows.end(),sortFlowsByArrtime);
for (auto var : flows) {
cout << var.id << " " << var.bandwidth << " " << var.arrTime << " " << var.needTime << endl;
}
It breaks unexpectedly at sort
this line every time.
Change return x.arrTime <= y.arrTime;
it to return x.arrTime < y.arrTime;
, and it will be resolved.
Reference link: https://www.coder.work/article/2746620