• <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>

            C小加

            厚德 博學(xué) 求真 至善 The bright moon and breeze
            posts - 145, comments - 195, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            hash_map,顧名思義,就是利用hash_set存儲結(jié)構(gòu)的寫map映照容器,普通的map用的是紅黑樹存儲結(jié)構(gòu)寫的。元素的檢索時(shí)間對比,hash_set近似為O(1),紅黑樹為O(logn)Hash_map的空間開銷要比map 的空間開銷大,尤其是我用數(shù)組模擬指針寫的hash_map

            所以,如果有必要采取映射結(jié)構(gòu)的時(shí)候,能用hash_map就別用map

            需要注意的是,hash_map遍歷出來的元素不是有序的。

            Hash_map和普通hash_set相比的話,hash_maphash_set多了一張value表,也就是映射表。

             

            下面是hash_map的模板,和hash_set的模板差不多。

            template<class KeyType,int MaxHash>
            struct HashFunction
            {
                int operator()(const KeyType& key)
                {
                    int h=key%MaxHash;
                    if (h<0) h+=MaxHash;
                    return h;
                }
            };

            template<class KeyType,class ValueType,int KeyContain,int MaxHash,class HashFun=HashFunction<KeyType,MaxHash> >
            class HashMap
            {
            public:
                HashMap():hashfun(){Clear();}
                ValueType& operator[](const KeyType& key)
                {
                    int h=hashfun(key);
                    int pos=head[h];
                    for(;pos!=0;pos=next[pos])
                    {
                        if(keys[pos]==key)
                            return values[pos];
                    }
                    next[++top]=head[h];
                    head[h]=top;
                    keys[top]=key;
                    values[top]=ValueType();//初始化
                    ++sz;
                    return values[top];
                }
                void Clear()
                {
                    memset(head,0,sizeof(head));
                    memset(next,0,(top+1)*sizeof(int));
                    top=0;
                    sz=0;
                }
                unsigned int size() const {return sz;}

            private:
                unsigned int sz;
                HashFun hashfun;
                int head[MaxHash];
                int next[KeyContain];
                KeyType keys[KeyContain];
                ValueType values[KeyContain];
                int top;
            };

             

            Feedback

            # re: hash_map哈希映照容器的實(shí)現(xiàn)  回復(fù)  更多評論   

            2012-04-19 16:15 by 嵌入式培訓(xùn)
            很好的方法

            # re: hash_map哈希映照容器的實(shí)現(xiàn)  回復(fù)  更多評論   

            2012-04-19 17:06 by SunRise_at
            寫的不錯(cuò)。。。。

            # re: hash_map哈希映照容器的實(shí)現(xiàn)  回復(fù)  更多評論   

            2012-05-10 19:31 by fanxixian
            能寫一下注釋嗎?
            国产精品无码久久综合网| 亚洲а∨天堂久久精品9966| 亚洲国产成人精品久久久国产成人一区二区三区综| 国产精品99久久99久久久| 久久99毛片免费观看不卡| 99热都是精品久久久久久| 97久久婷婷五月综合色d啪蜜芽| 欧美牲交A欧牲交aⅴ久久| 久久国产综合精品五月天| 色综合久久久久无码专区| 国产精品va久久久久久久| 77777亚洲午夜久久多喷| 亚洲综合精品香蕉久久网97 | 亚洲综合伊人久久综合| 99久久99久久精品国产片| 久久这里都是精品| 久久夜色精品国产亚洲| 久久精品人人做人人妻人人玩| 热久久国产欧美一区二区精品| 99久久国产综合精品成人影院| 人妻丰满AV无码久久不卡| 国产精品中文久久久久久久| 国产高潮久久免费观看| 99久久无码一区人妻| 精品久久久久久亚洲精品| 久久亚洲中文字幕精品有坂深雪| 久久国产精品免费一区二区三区| 精品蜜臀久久久久99网站| 亚洲国产精品久久电影欧美| 狠狠色噜噜色狠狠狠综合久久 | 亚洲综合日韩久久成人AV| 久久夜色精品国产噜噜亚洲a| 久久精品不卡| 久久久精品久久久久久| 久久久久亚洲精品男人的天堂| 日本一区精品久久久久影院| 色偷偷888欧美精品久久久| 国产精品美女久久久免费| 久久久久亚洲av成人无码电影 | 久久无码专区国产精品发布| 亚洲日本久久久午夜精品|