我有一个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;
}
return true;}
下面是对排序函数的调用。
currentNode->edgeList.sort(compareEdges);请帮助我删除此例外。

谢谢
发布于 2011-12-16 17:48:25
当两个相关字段相等时,比较器返回true。这是无效的,因此它很可能是sort实现通过assert检测到的。
您应该向sort传递一个“小于”谓词:形式上是一个“严格弱顺序”。其他的都是未定义的行为。在这种情况下,似乎你很幸运,并且实现检测到由于不一致的比较而进入了不可能的情况。
https://stackoverflow.com/questions/8532224
复制相似问题