无效运算符<同时排序std :: list [英] invalid operator < while sorting std::list
问题描述
边以目标节点为其成员。
bool compareEdges(const Edge * e1,const Edge * e2){
if(e1-> destination-> outdegree< e2-> destination-> outdegree){
return true;
}
else if(e1-> destination-> outdegree> e2-> destination-> outdegree){
return false;
}
else if(e1-> destination-> indegree> e2-> destination-> indegree){
return false;
}
返回true;
}
这里是电话到排序函数。
currentNode-> edgeList.sort(compareEdges);
请帮助我删除此例外。
谢谢
当两个相关字段相等时,您的比较器返回 true
这是无效的,所以可能通过断言检测到 sort
实现。
你应该将少于谓词传递给 sort
:正式是严格的弱订单。其他任何事情都是未定义的行为。在这种情况下,您似乎很幸运,由于比较不一致,实施过程会检测到它已经陷入不可能的情况。
I have a std::list graph edges and i want to sort the edges based on their destination outdegree and then their indegree. But i am getting getting exception of invalid operator < during my comparison function below is my code. My list contains the pointers to the edges and edges have destination nodes as their member.
bool compareEdges(const Edge *e1,const Edge *e2){
if(e1->destination->outdegree < e2->destination->outdegree){
return true;
}
else if(e1->destination->outdegree > e2->destination->outdegree){
return false;
}
else if(e1->destination->indegree > e2->destination->indegree){
return false;
}
return true;
}
And here is the call to the sort function.
currentNode->edgeList.sort(compareEdges);
Please help me in removing this exception.
Thanks
Your comparator returns true
when both relevant fields are equal. This is invalid, so it may well be what the sort
implementation has detected via assert.
You're supposed to pass a "less than" predicate to sort
: formally a "strict weak order". Anything else is undefined behavior. It seems in this case you got lucky, and the implementation detects that it has got into an impossible situation due to inconsistent comparisons.
这篇关于无效运算符<同时排序std :: list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!