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

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

问题描述

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

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;

}

到排序函数。

currentNode->edgeList.sort(compareEdges);

请帮助我删除此异常。

感谢

推荐答案

当两个相关字段相等时,您的比较器返回 true 。这是无效的,因此它可能是通过assert检测到的排序实现。

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.

传递一个小于的谓词到 sort :形式上是一个严格弱命令。任何其他都是未定义的行为。在这种情况下,你看起来很幸运,并且实现检测到由于不一致的比较,它已经发生了不可能的情况。

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天全站免登陆