无效运算符<同时排序std :: list [英] invalid operator < while sorting std::list

查看:164
本文介绍了无效运算符<同时排序std :: list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个std :: list图边缘,我想根据它们的目的地outdegree,然后他们的indegree排序边。但是我得到无效运算符的例外在我的比较功能下面是我的代码。我的列表包含指向边的指针,
边以目标节点为其成员。

  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.

这篇关于无效运算符&lt;同时排序std :: list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆