简体   繁体   English

“无效比较器”:重载“<”运算符时出错

[英]“invalid comparator” : error when overloading the “<” operator

I have a class that needs to be sorted. 我有一个需要排序的类。 Using a vector of this class, I get the error "invalid comparator" while sorting. 使用这个类的向量,我在排序时得到错误“无效比较器”。

I have overloaded the "<" operator in my class and followed strict weak ordering . 我在我的课程中重载了“<”运算符,并遵循严格的弱顺序

As mentioned in this post . 在本提到的职位

sort requires a strict weak ordering. sort需要严格的弱排序。 Your comparator isn't one. 你的comparator不是一个。 Among many other things, for a strict weak ordering, comp(x, x) must be false . 在许多其他事情中,对于严格的弱排序, comp(x, x)必须是假的

This is my Code: 这是我的代码:

bool outlierScore::operator<(const outlierScore& other) {
    if (score < other.score)
        return score < other.score;
    else if (coreDistance < other.coreDistance)
        return coreDistance < other.coreDistance;
    else if (id < other.id)
        return id < other.id;
    else
        return false;
}

this is the overloaded operator function, what it does essentially is trying to sort in ascending order by outlier score, with core distances used to break outlier score ties, and ids used to break core distance ties. 这是重载的运算符函数,它本质上是试图按异常值得分按升序排序,核心距离用于打破离群值得分关系,而id用于打破核心距离关系。

Stack Trace revealed the error coming at this stage. Stack Trace揭示了此阶段的错误。

template <class _Pr, class _Ty1, class _Ty2>
constexpr bool _Debug_lt_pred(_Pr&& _Pred, _Ty1&& _Left, _Ty2&& _Right) _NOEXCEPT_COND(
    noexcept(_Pred(_Left, _Right))
    && noexcept(_Pred(_Right, _Left))) { // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
    const auto _Result = static_cast<bool>(_Pred(_Left, _Right));
    if (_Result) {
        _STL_VERIFY(!_Pred(_Right, _Left), "invalid comparator");
    }

    return _Result;
}

I am unable to find the issue. 我无法找到问题。 Any help would be great. 任何帮助都会很棒。

The minimal reproducible example : 最小可重复的例子

class outlierScore
{
private:
    double coreDistance;
public:
    double score;
    int id;
}
vector <vector<double>> x = {
                {0,0.889528896739179,0.536626916823739},
                {1,1.30766765703343,0.684794721931497},
                {2,0.936505261432846,0.559870334496815}
            };
vector<outlierScore> test;
test.push_back(outlierScore(x[0][2], x[0][1], x[0][0]));
test.push_back(outlierScore(x[1][2], x[1][1], x[1][0]));
test.push_back(outlierScore(x[2][2], x[2][1], x[2][0]));

contains outlierScore Objects which look like {id, coreDistance, score} 包含看起来像{id, coreDistance, score} outlierScore对象

Where it gives an error: sort(test.begin(), test.end()); 它给出错误的地方: sort(test.begin(), test.end());

Your implementation is not correct. 您的实施不正确。

bool outlierScore::operator<(const outlierScore& other) const {
    return (score < other.score) ||
           (score == other.score && coreDistance < other.coreDistance) ||
           (score == other.score && coreDistance == other.coreDistance && id < other.id);
}

Or 要么

bool outlierScore::operator<(const outlierScore& other) const {
    return std::tie(score, coreDistance, id) < std::tie(other.score, other.coreDistance, other.id);
}

Other than the method is not const , this operator does not satisfy strict weak ordering. 除了方法不是const ,该运算符不满足严格的弱排序。

bool outlierScore::operator<(const outlierScore& other) {
    if (score < other.score)
        return score < other.score;
    else if (coreDistance < other.coreDistance)
        return coreDistance < other.coreDistance;
    else if (id < other.id)
        return id < other.id;
    else
        return false;
}

For example, if all fields are are integers. 例如,如果所有字段都是整数。

a.score = 0 ; a.coreDistance = 1
b.score = 1 ; b.coreDistance = 0

For these values, the following should be false, but a true is returned: 对于这些值,以下内容应为false,但返回true:

a < b && b < a

You should also check equality: 你还应该检查相等性:

bool outlierScore::operator<(const outlierScore& other) {
    if (score != other.score)
        return score < other.score;
    else if (coreDistance != other.coreDistance)
        return coreDistance < other.coreDistance;
    else 
        return id < other.id;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM