溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Expression:invalid comarator怎么辦

發(fā)布時(shí)間:2021-11-22 15:23:13 來(lái)源:億速云 閱讀:126 作者:小新 欄目:編程語(yǔ)言

這篇文章給大家分享的是有關(guān)Expression:invalid comarator怎么辦的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

場(chǎng)景

        定義函數(shù)如下:

template <class T> struct greaterkey {//用于指定map當(dāng)中的排序,遞減,map中默認(rèn)遞增
 bool operator() (const T& x, const T& y) const
 {
  return x > y;
 }
};
template <class T> struct lesskey {
 bool operator() (const T& x, const T& y) const
 {
  return x < y;
 }
};

解決

vs2010后都是嚴(yán)格比較,相等的兩個(gè)元素,一定要返回false,可以看到STL的源碼:
[cpp]
template<class _Pr, class _Ty1, class _Ty2> inline
    bool _Debug_lt_pred(_Pr _Pred,
        _Ty1& _Left, _Ty2& _Right,
        _Dbfile_t _File, _Dbline_t _Line)
    {   // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
    if (!_Pred(_Left, _Right))
        return (false);
    <strong>else if (_Pred(_Right, _Left))
        _DEBUG_ERROR2("invalid operator<", _File, _Line);</strong>
    return (true);
    }
如果left和right都一樣,STL就會(huì)報(bào)錯(cuò)。。所以只能修改下自己的代碼,當(dāng)相等的時(shí)候,就返回false了

template <class T> struct greaterkey {//用于指定map當(dāng)中的排序,遞減,map中默認(rèn)遞增
 bool operator() (const T& x, const T& y) const
 {

    if (x == y) return false;
      return x > y;
 }
};
template <class T> struct lesskey {
 bool operator() (const T& x, const T& y) const
 {

    if (x == y) return false;

      return x < y;
 }
};


感謝各位的閱讀!關(guān)于“Expression:invalid comarator怎么辦”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI