Abd*_*mad 20 c++ sorting stl exception 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;
}
return true;
Run Code Online (Sandbox Code Playgroud)
}
这是对sort函数的调用.
currentNode->edgeList.sort(compareEdges);
Run Code Online (Sandbox Code Playgroud)
请帮我删除此例外.

谢谢
Ste*_*sop 33
true当两个相关字段相等时,比较器返回.这是无效的,因此很可能是sort实现通过断言检测到的.
你应该把一个"小于"的谓词传递给sort:正式的"严格的弱秩序".其他任何东西都是未定义的行为.在这种情况下,你似乎很幸运,并且实现检测到它由于不一致的比较而陷入了不可能的情况.