簡體   English   中英

“無效比較器”:重載“<”運算符時出錯

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

我有一個需要排序的類。 使用這個類的向量,我在排序時得到錯誤“無效比較器”。

我在我的課程中重載了“<”運算符,並遵循嚴格的弱順序

在本提到的職位

sort需要嚴格的弱排序。 你的comparator不是一個。 在許多其他事情中,對於嚴格的弱排序, comp(x, x)必須是假的

這是我的代碼:

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;
}

這是重載的運算符函數,它本質上是試圖按異常值得分按升序排序,核心距離用於打破離群值得分關系,而id用於打破核心距離關系。

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;
}

我無法找到問題。 任何幫助都會很棒。

最小可重復的例子

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]));

包含看起來像{id, coreDistance, score} outlierScore對象

它給出錯誤的地方: sort(test.begin(), test.end());

您的實施不正確。

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);
}

要么

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

除了方法不是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;
}

例如,如果所有字段都是整數。

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

對於這些值,以下內容應為false,但返回true:

a < b && b < a

你還應該檢查相等性:

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