• <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>
            aurain
            技術文摘
            posts - 137,  comments - 268,  trackbacks - 0

            sgi stl中的hash_map使用請參加http://www.stlchina.org/twiki/bin/view.pl/Main/STLDetailHashMap
            在vc中(本文以vc2003為編譯環境),hash_map與sgi stl中的定義是不一樣的,區別如下:
            vc:
            template<class _Kty,
             class _Ty,
             class _Tr = hash_compare<_Kty, _STD less<_Kty> >,
             class _Alloc = _STD allocator< _STD pair<const _Kty, _Ty> > >
             class hash_map
            {

            }
            sgi:
            template <class _Key, class _Tp, class _HashFcn = hash<_Key>,
            class _EqualKey = equal_to<_Key>,
            class _Alloc = __STL_DEFAULT_ALLOCATOR(_Tp) >
            class hash_map
            {
            }
            可以看出,vc中把hash函數和比較函數(并且是“嚴格弱小于”)都整合到一起了,就是hash_compare,而在sgi中,兩者是分開的,分別是兩個不同的仿函數。
            在這里,我們舉一個在vs中用hash_map保存自定義類的例子,關鍵之處是定義合適的hash_compare。
            詳情看代碼~~

            //1 define the class
            class ClassA{
            public:
             ClassA(int a):m_a(a){}
             int getvalue()const { return m_a;}
             void setvalue(int a){m_a;}
            private:
             int m_a;
            };

            //2 define the hash function
            struct my_hash_compare : public stdext::hash_compare<ClassA>

             size_t operator()(const ClassA & A)const
             {
              return A.getvalue();
             }

             bool operator()(const ClassA & a1, const ClassA & a2)const
            {
              return  a1.getvalue() < a2.getvalue();
             }
            };

            int _tmain(int argc, _TCHAR* argv[])
            {
              hash_map<ClassA, string, my_hash_compare> hmap;
             ClassA a1(1);
             ClassA a2(2);

             hmap[a1] = "a";
             hmap[a2] = "b";

             ClassA a3(1);
             if (hmap.find(a3) != hmap.end())
             {
              cout << "find ok" << endl;
             }
            return 0;
            }

            posted on 2009-04-29 14:00 閱讀(2749) 評論(3)  編輯 收藏 引用 所屬分類: vc

            FeedBack:
            # re: 在visual c++ 2003中用hash_map保存自定義類型數據
            2009-04-29 20:29 | 空明流轉
            std::tr1::unordered_map,boost里面有的。  回復  更多評論
              
            # re: 在visual c++ 2003中用hash_map保存自定義類型數據
            2009-12-29 20:15 | Nova
            代碼根本就是錯的,
            bool operator()(const ClassA & a1, const ClassA & a2)const
            {
            return a1.getvalue() == a2.getvalue();
            }
            應該改為

            bool operator()(const ClassA & a1, const ClassA & a2)const
            {
            return a1.getvalue() < a2.getvalue();
            }
            發表的時候自己運行一下,不然害人不淺~  回復  更多評論
              
            # re: 在visual c++ 2003中用hash_map保存自定義類型數據
            2009-12-31 08:51 |
            @Nova
            謝謝你的提醒,確實是需要這樣改  回復  更多評論
              

            <2010年11月>
            31123456
            78910111213
            14151617181920
            21222324252627
            2829301234
            567891011

            常用鏈接

            留言簿(17)

            隨筆分類(138)

            隨筆檔案(137)

            網絡開發

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 498816
            • 排名 - 36

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久人妻一区精品| 国内精品伊人久久久久av一坑| 欧美va久久久噜噜噜久久| 国产成人精品免费久久久久| 99久久婷婷国产综合亚洲| 国产成人精品久久一区二区三区| 99久久精品国产一区二区三区| 久久久久一本毛久久久| 日产精品久久久一区二区| 久久精品?ⅴ无码中文字幕| 久久这里只有精品18| 国产成人精品久久| 7777久久亚洲中文字幕| 欧美激情精品久久久久久久九九九| 人妻无码中文久久久久专区| 久久99精品免费一区二区| 亚洲AV日韩精品久久久久久久| 久久精品免费大片国产大片| 久久精品一区二区国产| 久久久噜噜噜久久熟女AA片| 久久久久久午夜精品| 久久天天日天天操综合伊人av| 国产叼嘿久久精品久久| 久久黄色视频| 亚洲中文字幕无码一久久区| 亚洲国产成人久久一区WWW| 久久丫忘忧草产品| 97久久精品无码一区二区| 蜜桃麻豆www久久| 中文字幕日本人妻久久久免费 | 久久亚洲美女精品国产精品| 无码国内精品久久人妻| 日本福利片国产午夜久久| 久久国产精品国语对白| 久久久噜噜噜久久熟女AA片| 国产精品gz久久久| AV狠狠色丁香婷婷综合久久| 中文国产成人精品久久亚洲精品AⅤ无码精品| 久久亚洲日韩看片无码| 久久天堂电影网| 青青草国产成人久久91网|