首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

遍历复杂的Hash Map(非STL的hash_map)的元素有关问题

2012-03-02 
遍历复杂的Hash Map(非STL的hash_map)的元素问题如下一个hashmap抽象模板:如果我需要遍历整个hashmap,该如

遍历复杂的Hash Map(非STL的hash_map)的元素问题
如下一个hash   map抽象模板:
如果我需要遍历整个hash   map,该如何声明一个iterator?

例如:   HashMap <String,   Studeng*> ::iterator   itr;
结果编译错误,为什么,语法上有那些问题?

#include   "HashTable.h "
template <typename   PairType>   struct   PairFirstExtractor;
template <typename   KeyArg,   typename   MappedArg,   typename   HashArg   =   typename   DefaultHash <KeyArg> ::Hash,
              typename   KeyTraitsArg   =   HashTraits <KeyArg> ,   typename   MappedTraitsArg   =   HashTraits <MappedArg>   >

class   HashMap   {
        private:
                typedef   KeyTraitsArg   KeyTraits;
                typedef   MappedTraitsArg   MappedTraits;
                typedef   PairBaseHashTraits <KeyTraits,   MappedTraits>   ValueTraits;

        public:
                typedef   typename   KeyTraits::TraitType   KeyType;
                typedef   typename   MappedTraits::TraitType   MappedType;
                typedef   typename   ValueTraits::TraitType   ValueType;

        private:
                typedef   HashArg   HashFunctions;

                typedef   typename   HashKeyStorageTraits <HashFunctions,   KeyTraits> ::Hash   StorageHashFunctions;

                typedef   typename   HashKeyStorageTraits <HashFunctions,   KeyTraits> ::Traits   KeyStorageTraits;
                typedef   typename   MappedTraits::StorageTraits   MappedStorageTraits;
                typedef   PairHashTraits <KeyStorageTraits,   MappedStorageTraits>   ValueStorageTraits;

                typedef   typename   KeyStorageTraits::TraitType   KeyStorageType;
                typedef   typename   MappedStorageTraits::TraitType   MappedStorageType;
                typedef   typename   ValueStorageTraits::TraitType   ValueStorageType;

                typedef   HashTable <KeyStorageType,   ValueStorageType,   PairFirstExtractor <ValueStorageType> ,
                        StorageHashFunctions,   ValueStorageTraits,   KeyStorageTraits>   HashTableType;

        public:
                typedef   HashTableIteratorAdapter <HashTableType,   ValueType>   iterator;
                typedef   HashTableConstIteratorAdapter <HashTableType,   ValueType>   const_iterator;

                HashMap();
                HashMap(const   HashMap&);


                HashMap&   operator=(const   HashMap&);
                ~HashMap();

                int   size()   const;
                int   capacity()   const;
                bool   isEmpty()   const;

                //   iterators   iterate   over   pairs   of   keys   and   values
                iterator   begin();
                iterator   end();
                const_iterator   begin()   const;
                const_iterator   end()   const;

                iterator   find(const   KeyType&);
                const_iterator   find(const   KeyType&)   const;
                bool   contains(const   KeyType&)   const;
                MappedType   get(const   KeyType&)   const;

                //   replaces   value   but   not   key   if   key   is   already   present
                //   return   value   is   a   pair   of   the   iterator   to   the   key   location,  
                //   and   a   boolean   that 's   true   if   a   new   value   was   actually   added
                pair <iterator,   bool>   set(const   KeyType&,   const   MappedType&);  

                //   does   nothing   if   key   is   already   present
                //   return   value   is   a   pair   of   the   iterator   to   the   key   location,  
                //   and   a   boolean   that 's   true   if   a   new   value   was   actually   added
                pair <iterator,   bool>   add(const   KeyType&,   const   MappedType&);  

                void   remove(const   KeyType&);
                void   remove(iterator   it);
                void   clear();

        private:
                pair <iterator,   bool>   inlineAdd(const   KeyType&,   const   MappedType&);
                void   refAll();
                void   derefAll();


                HashTableType   m_impl;
        };


[解决办法]
嗯?你没用stl扩展里的hash_map,用的是自己定义的?
出啥错?
[解决办法]
iterator已经集成到HashMap类里了,使用时直接用begin,end等
[解决办法]
算了,那就别搞那么复杂了,直接用stl的hash_map替换就可以了。
[解决办法]
呃,真会自己反复造轮子呀。shared_ptr在boost里也是现成的。
用stl的hash_map,你也一样可以存入RefPtr <COOKIE> /shared_ptr <COOKIE> 嘛,怎么会一个对象要保存多份呢。
[解决办法]
你现在的错误,提示得也很清楚呀,HashTableIteratorAdapter没有默认构造函数。
[解决办法]
先换个编译器试一下,
确认是编译器问题还是程序问题。
毕竟涉及到模板错误,一般编译器都支持的不好,
编译通不过并不意味着程序语法是错误的。
[解决办法]
第一,如果不是海量数据,hash_map基本没有性能优势,还不如使用map
第二,hash_map/map如果不是作为数据中心,使用引用计数只会造成更加严重的“已释放对象仍存在”的语意级错误
第三,VC2005对模板的正常非常好,绝不弱于gcc。

热点排行