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

            sgi stl中的hash_map使用請參加http://www.stlchina.org/twiki/bin/view.pl/Main/STLDetailHashMap
            在vc中(本文以vc2003為編譯環(huán)境),hash_map與sgi stl中的定義是不一樣的,區(qū)別如下:
            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函數(shù)和比較函數(shù)(并且是“嚴(yán)格弱小于”)都整合到一起了,就是hash_compare,而在sgi中,兩者是分開的,分別是兩個(gè)不同的仿函數(shù)。
            在這里,我們舉一個(gè)在vs中用hash_map保存自定義類的例子,關(guān)鍵之處是定義合適的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 閱讀(2751) 評論(3)  編輯 收藏 引用 所屬分類: vc

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

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

            <2012年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            常用鏈接

            留言簿(17)

            隨筆分類(138)

            隨筆檔案(137)

            網(wǎng)絡(luò)開發(fā)

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 499407
            • 排名 - 36

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久亚洲AV无码永不| 久久精品国产第一区二区三区| 久久人人爽人爽人人爽av | 中文精品99久久国产| 国产精品女同一区二区久久| 久久久久综合国产欧美一区二区| 久久久久亚洲AV片无码下载蜜桃 | 久久久亚洲裙底偷窥综合| 久久精品麻豆日日躁夜夜躁| 精品久久久久久久久久久久久久久 | 亚洲精品无码久久不卡| 99国产精品久久| 2021久久精品免费观看| 国产精品女同一区二区久久| 99久久人妻无码精品系列蜜桃| 亚洲AⅤ优女AV综合久久久| 久久免费视频观看| 亚洲国产精品久久电影欧美| 亚洲国产精品无码久久久蜜芽| 久久久久九国产精品| 青青热久久国产久精品| 国产精品久久久久久影院| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 奇米影视7777久久精品| 思思久久99热免费精品6| 国产成人精品久久亚洲高清不卡| 久久国产精品无码一区二区三区| 国产精品美女久久福利网站| 久久久久亚洲av毛片大| 久久久久无码精品| 久久天天躁狠狠躁夜夜av浪潮| 99久久精品免费看国产免费| 久久综合狠狠色综合伊人| 久久亚洲高清观看| 亚洲一区二区三区日本久久九| aaa级精品久久久国产片| 久久国产亚洲高清观看| 精品国产91久久久久久久| 久久人人爽人人爽人人片av高请| 久久一日本道色综合久久| 国内精品久久久久伊人av|