• <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).
            狠狠久久综合伊人不卡| 亚洲欧美国产精品专区久久| 久久免费的精品国产V∧| 久久精品中文无码资源站| 久久综合狠狠色综合伊人| 久久99热精品| 久久免费香蕉视频| 亚洲精品乱码久久久久久按摩 | 久久国产欧美日韩精品免费| 久久亚洲国产精品五月天婷| 久久久久久久97| 97r久久精品国产99国产精| AA级片免费看视频久久| 久久婷婷国产剧情内射白浆| 久久99国产精品一区二区| 久久99热这里只频精品6| 久久91精品久久91综合| 精品久久亚洲中文无码| 国产成人AV综合久久| 亚洲AV无码久久精品蜜桃| 久久se精品一区精品二区国产| 久久香综合精品久久伊人| 久久综合久久久| 久久久久久久人妻无码中文字幕爆 | 亚洲精品国产综合久久一线| 国内精品久久久久| 亚洲色大成网站www久久九| 国产激情久久久久影院老熟女免费 | 久久综合给久久狠狠97色| 九九热久久免费视频| 97久久精品人人做人人爽| 91久久婷婷国产综合精品青草| 久久久久久久波多野结衣高潮 | 日韩中文久久| Xx性欧美肥妇精品久久久久久| 99久久精品影院老鸭窝| 色婷婷综合久久久久中文 | 久久无码人妻一区二区三区午夜| 四虎影视久久久免费| 久久精品中文字幕有码| 国产精品无码久久四虎|