• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運轉,開心的工作
            簡單、開放、平等的公司文化;尊重個性、自由與個人價值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            小于號重載須注意的問題

            Posted on 2011-10-15 07:50 S.l.e!ep.¢% 閱讀(742) 評論(0)  編輯 收藏 引用 所屬分類: C++
            這是今天寫程序中遇到的兩個詭異的問題。我的 IDE 是 VC++2005 ExpressiEdition 。
              第一個問題是關于 map 的。話不多說,以下 20 多行的 C++ 代碼重現了我遇到的問題:
              #include <iostream>
              #include <map>
              using namespace std;
              struct S {
              int x, y;
              S(int xx, int yy): x(xx), y(yy) {}
              bool operator <(const S& s) const {
              return x < s.x && y < s.y;
              }
              };
              map<S, int > ms;
              int main() {
              ms.insert(map<S, int >::value_type(S(31, 41), 59));
              S test(31, 59);
              if (ms.find(test) != ms.end()) {
              cout << "Find the value: " << ms[test] << endl;
              } else {
              cout << "Find Failure\n" ;
              }
              return 0;
              }
              使用 VC++6.0 , VC++2005 Express Edition, VC++2005 command line compiler( 不帶任何編譯選項 ) , g++ 測試的結果都相同,最后輸出:
              Find the value: 59
              這個問題比較隱蔽。多個編譯器測試結果相同說明肯定不是編譯器版本相關的問題。直接調試進入 find 函數就可以明白:
              iterator find(const key_type& _Keyval)
              { // find an element in mutable sequence that matches _Keyval
              iterator _Where = lower_bound(_Keyval);
              return (_Where == end()
              || _DEBUG_LT_PRED(this ->comp,
              _Keyval, _Key(_Where._Mynode()))
              ? end() : _Where);
              }
              雖然這樣調試會遇到一些 STL 內部的細節,但整體實現思路還是可看出來。在 find 函數中, lower_bound 返回值是結點 (31, 41) 。跟蹤進入,發現調用的 _DEBUG_LT_PRED 的定義如下:
              #define _DEBUG_LT_PRED(pred, x, y) _Debug_lt_pred(pred, x, y, __FILEW__, __LINE__)
              template <class _Pr, class _Ty1, class _Ty2> inline
              bool __CLRCALL_OR_CDECL _Debug_lt_pred(_Pr _Pred, const _Ty1& _Left, const _Ty2& _Right,
              const wchar_t *_Where, unsigned int _Line)
              { // test if _Pred(_Left, _Right) and _Pred is strict weak ordering
              if (!_Pred(_Left, _Right))
              return (false );
              else if (_Pred(_Right, _Left))
              _DEBUG_ERROR2("invalid operator<" , _Where, _Line);
              return (true );
              }
              ( 注:關于 _Debug_lt_pred 函數有三個重載版本,分別是針對參數 _Left, _Right 的 const 性質的,看這些代碼能學到很多東西。另外,如果靜態地看這些代碼來分析自己程序中的錯誤,則因為有大量的重載函數,所以靜態分析時很難自己確定到底哪一個函數被調用,而動態調試就能一步到位。 )
              從這個函數的代碼里大致就能看出問題所在了。猜測這里的 _Pred 參數就是自己在 struct 里定義的那個 operator < ,編譯器中看到 _Pred 的 value 是 {lessthan } , type 是 std::less <S> ,但這里有更大的發現: strict weak ordering!!! 自己 C++ 功底很淺,這是一個新的發現,馬上 google 一下 ”strict weak ordering” 這個關鍵詞,果然發現大量的專題鏈接!暫且先放下這個主題。問題猜測肯定是出在 operator < 這個函數上了,因為根據自己的 operator < 定義: {31, 41} < {31, 59} 返回值是 false , {31, 59} < {31, 41} 的返回值也是 false ,那么,由這兩個比較能得出結論: {31, 41} == {31, 59} !!! 這也無怪乎程序運行會返回不期望的結果了。
              但這也只是猜測,繼續調試,看能不能找到 _Pred 函數的真實面目。上面說了從編譯器中看出 _Pred 的 type 是 std::less <S> ,在 MSDN 中找到 less 是 STL 中的一個模板類,以下是在 MSDN 中看到的定義:
              less
              less
              template<class T>
              struct less
              : public binary_function
              <T, T, bool> {
              bool operator()
              (const T& x, const T& y) const;
              };
              The template class defines its member function as returning x < y . The member function defines a total ordering , even if T is an object pointer type.
              我們接著調試,跟蹤進入 _Pred 函數,發現它的定義如下:
              template <class _Ty>
              struct less
              : public binary_function<_Ty, _Ty, bool >
              { // functor for operator<
              bool operator ()(const _Ty& _Left, const _Ty& _Right) const
              { // apply operator< to operands
              return (_Left < _Right);
              }
              };
              它最終比較 _Left 和 _Right 時調用的正是 struct S 中定義的 operator < 。
              至此,問題真相大白。還遺留兩個主題:一個是關于 strict weak ordering ,另一個是 STL 中的一些實現方法,因為以上只是跟蹤調試過程把沿途看到的東西機械地記錄了下來,并不是真正的理解。
              無獨有偶,今天遇到另一個問題,關于 STL 中的 sort 函數的問題,這個問題是唯獨在 VC++ 2005 Express Edition 中才出現的,并且在命令行下使用 cl.exe 不帶任何選項編譯連接時正常,使用 g++ 也正常。問題的表現就是程序在運行時出現異常,信息是: ”invalid operator <” 。這個問題就不再重現調試了,它的解決方法見下列地址:
              http://support.microsoft.com/kb/949171
              strict weak ordering 是一個數學上的術語,剛剛給出的這個地址上面有關于 strict weak ordering 的簡明的解釋,貼過來:
              The STL algorithms for stable_sort ( ) and sort() require the binary predicate to be strict weak ordering.
              For example:
              · Strict: pred (X, X) is always false.
              · Weak: If ! pred (X, Y) && !pred (Y, X), X==Y.
              · Ordering: If pred (X, Y) && pred (Y, Z), then pred (X, Z).
            国产成人精品久久免费动漫| 久久久久亚洲AV综合波多野结衣| 97精品伊人久久久大香线蕉| 97热久久免费频精品99| 久久午夜羞羞影院免费观看| 色欲久久久天天天综合网| 蜜桃麻豆WWW久久囤产精品| 亚洲国产精品狼友中文久久久 | 亚洲人成无码www久久久| 久久成人永久免费播放| 久久精品国产一区二区| 欧美精品一区二区久久 | 激情五月综合综合久久69| 国产成人精品久久一区二区三区 | 久久久久国产精品人妻| 久久精品国产2020| 国产午夜精品久久久久免费视| 97久久精品无码一区二区天美| 日本精品久久久中文字幕| 久久国产精品免费| 综合网日日天干夜夜久久| 九九久久99综合一区二区| 国产综合成人久久大片91| 欧美亚洲国产精品久久| 97热久久免费频精品99| 久久夜色撩人精品国产小说| 一本久道久久综合狠狠爱| 一本伊大人香蕉久久网手机| 国内精品伊人久久久影院| 久久精品国产一区| 一级a性色生活片久久无少妇一级婬片免费放 | 亚洲精品视频久久久| 香蕉久久夜色精品升级完成| 国产999精品久久久久久| 欧美日韩精品久久久免费观看| 久久99国产精品久久| 久久精品国产色蜜蜜麻豆| 精品久久久久久无码中文字幕| 人妻精品久久无码专区精东影业| 久久99精品久久久久久水蜜桃| 无遮挡粉嫩小泬久久久久久久 |