• <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 閱讀(2742) 評論(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
            謝謝你的提醒,確實是需要這樣改  回復  更多評論
              

            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            常用鏈接

            留言簿(17)

            隨筆分類(138)

            隨筆檔案(137)

            網絡開發

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 497507
            • 排名 - 36

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            国产午夜免费高清久久影院| 性高朝久久久久久久久久| 久久天天躁夜夜躁狠狠躁2022| 无码任你躁久久久久久老妇App| 亚洲AV伊人久久青青草原| 久久亚洲精品成人无码网站| 国产aⅴ激情无码久久| 国内精品久久久久影院一蜜桃| 2020最新久久久视精品爱 | 欧美一区二区三区久久综| 久久夜色精品国产噜噜亚洲AV | 精品多毛少妇人妻AV免费久久| 中文成人无码精品久久久不卡| 久久久久亚洲精品天堂| 精品免费久久久久国产一区| 久久精品国产乱子伦| 国产精品成人久久久久三级午夜电影 | 99精品久久精品一区二区| 久久精品成人免费观看97| 午夜精品久久久久成人| 久久91精品国产91久久户| 综合久久国产九一剧情麻豆| 99久久精品这里只有精品| 91精品国产综合久久精品| 久久久久久国产a免费观看黄色大片| 色综合久久精品中文字幕首页| 国产成人久久精品一区二区三区| 久久久久黑人强伦姧人妻| 久久无码av三级| 久久精品国产只有精品2020| 国产美女亚洲精品久久久综合| 久久久精品视频免费观看| 午夜不卡888久久| 嫩草影院久久国产精品| 久久久婷婷五月亚洲97号色| 无码AV波多野结衣久久| 伊人久久大香线蕉av一区| 无码人妻久久一区二区三区 | 熟妇人妻久久中文字幕| 久久乐国产综合亚洲精品| 久久婷婷国产剧情内射白浆 |