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

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

            hash_map哈希映照容器的實現

            Posted on 2012-04-18 20:18 C小加 閱讀(2465) 評論(3)  編輯 收藏 引用 所屬分類: 數據結構和算法模板

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

            所以,如果有必要采取映射結構的時候,能用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哈希映照容器的實現  回復  更多評論   

            2012-04-19 16:15 by 嵌入式培訓
            很好的方法

            # re: hash_map哈希映照容器的實現  回復  更多評論   

            2012-04-19 17:06 by SunRise_at
            寫的不錯。。。。

            # re: hash_map哈希映照容器的實現  回復  更多評論   

            2012-05-10 19:31 by fanxixian
            能寫一下注釋嗎?
            久久高清一级毛片| 久久综合精品国产二区无码| 国产精品热久久毛片| 久久久精品久久久久久 | 久久婷婷五月综合国产尤物app| 综合人妻久久一区二区精品| 久久这里只精品国产99热| 久久艹国产| 日韩精品无码久久久久久| 国产亚洲精午夜久久久久久| 国产亚洲美女精品久久久2020| 2020最新久久久视精品爱| 亚洲午夜久久久影院伊人| 久久久国产精品| 97久久精品人人澡人人爽| 久久精品国产亚洲AV嫖农村妇女| 久久久久亚洲精品男人的天堂| 久久综合九色综合网站| 欧美日韩精品久久久久| 精品国产婷婷久久久| 国产69精品久久久久777| 亚洲人成网亚洲欧洲无码久久 | 伊人久久大香线焦综合四虎| 99精品久久久久久久婷婷| 亚洲欧美国产精品专区久久| 青青草国产精品久久久久| 国产午夜精品理论片久久影视 | 2021久久精品免费观看| 狠狠精品久久久无码中文字幕 | 亚洲国产精品久久久久婷婷软件 | 欧美一区二区精品久久| 精品少妇人妻av无码久久| 97精品依人久久久大香线蕉97| 久久综合伊人77777| 久久青青草原精品国产软件| 久久久久久无码国产精品中文字幕| 亚洲午夜精品久久久久久人妖| 伊人久久大香线焦综合四虎| 国产无套内射久久久国产| 久久精品国产亚洲精品| 热综合一本伊人久久精品|