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

zengwh 2007-04-13 10:32:28
如下一个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;
};
...全文
1858 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
taodm 2007-04-13
  • 打赏
  • 举报
回复
第一,如果不是海量数据,hash_map基本没有性能优势,还不如使用map
第二,hash_map/map如果不是作为数据中心,使用引用计数只会造成更加严重的“已释放对象仍存在”的语意级错误
第三,VC2005对模板的正常非常好,绝不弱于gcc。
zengwh 2007-04-13
  • 打赏
  • 举报
回复
看来只能自己写一个HashTableIteratorAdapter默认的构造函数试试;

用的GCC4.0.3编译的,其他的模块都正确;

HashMap类型原来已经存在, 这里只是想遍历这个HashMap实例,将key和value存到文件.;

HashMap中的key和value来自于其他模块的数据,这里只是为了方便查找/插入等操作
也就是将对象的计数进行加减,如果不用这个HashMap,这可能会非法引用已释放对象的
.

VC对于模板的支持不如GCC强.
Jofee 2007-04-13
  • 打赏
  • 举报
回复
先换个编译器试一下,
确认是编译器问题还是程序问题。
毕竟涉及到模板错误,一般编译器都支持的不好,
编译通不过并不意味着程序语法是错误的。
taodm 2007-04-13
  • 打赏
  • 举报
回复
你现在的错误,提示得也很清楚呀,HashTableIteratorAdapter没有默认构造函数。
taodm 2007-04-13
  • 打赏
  • 举报
回复
呃,真会自己反复造轮子呀。shared_ptr在boost里也是现成的。
用stl的hash_map,你也一样可以存入RefPtr<COOKIE>/shared_ptr<COOKIE>嘛,怎么会一个对象要保存多份呢。
zengwh 2007-04-13
  • 打赏
  • 举报
回复
在线急等.....
zengwh 2007-04-13
  • 打赏
  • 举报
回复
我也想,不过这个系统的底层都是在自定义的模板类RefPtr-抽象的引用指针类型基础上
对象的释放通过引用计数控制.
如果不用这个HashMap,对于一个对象则需要保存多份内存. 这种以消耗内存方式为代价实现基本功能
在目前的受限制.
主要是我就想不明白,采用以下方式声明一个变量是正确的:
HashMap<String, COOKIE*> COOKIEJAR;
而引用iterator类型声明变量,在展开模板时提示上述错误:
HashMap<String, COOKIE*>::iterator itr;
taodm 2007-04-13
  • 打赏
  • 举报
回复
算了,那就别搞那么复杂了,直接用stl的hash_map替换就可以了。
zengwh 2007-04-13
  • 打赏
  • 举报
回复
是别人抽象写的,我需要维护,这个hash map不是STL的hash map,主要问题是
定义一个迭代器变量,编译器在展开模板后错误.如:HashMap<String, COOKIE*>::iterator itr
tr;
编译器错误如下:
CookieManager.cpp: In member function ‘int WebCore::CookieManager::saveCookies()’:
CookieManager.cpp:249: error: no matching function for call to
‘WTF::HashTableIteratorAdapter<
WTF::HashTable<WebCore::StringImpl*, std::pair<WebCore::StringImpl*, int>,
WTF::PairFirstExtractor<std::pair<WebCore::StringImpl*, int> >,
WTF::StrHash<WebCore::StringImpl*>,
WTF::PairHashTraits<WTF::HashTraits<WebCore::StringImpl*>, WTF::HashTraits<int> >,
WTF::HashTraits<WebCore::StringImpl*> >,
std::pair<WebCore::String, WebCore::COOKIE*> >::HashTableIteratorAdapter()’

/home/whzeng/mdolphin/source/wtf/HashTable.h:797: note: candidates are:
WTF::HashTableIteratorAdapter<HashTableType, ValueType>::
HashTableIteratorAdapter(const typename HashTableType::iterator&)
[with HashTableType = WTF::HashTable<WebCore::StringImpl*,
std::pair<WebCore::StringImpl*, int>,
WTF::PairFirstExtractor<std::pair<WebCore::StringImpl*, int> >,
WTF::StrHash<WebCore::StringImpl*>,
WTF::PairHashTraits<WTF::HashTraits<WebCore::StringImpl*>,WTF::HashTraits<int> >,
WTF::HashTraits<WebCore::StringImpl*> >,
ValueType = std::pair<WebCore::String, WebCore::COOKIE*>]

/home/whzeng/mdolphin/source/wtf/HashTable.h:796: note:
WTF::HashTableIteratorAdapter<
WTF::HashTable<WebCore::StringImpl*,
std::pair<WebCore::StringImpl*, int>,
WTF::PairFirstExtractor<std::pair<WebCore::StringImpl*, int> >,
WTF::StrHash<WebCore::StringImpl*>,
WTF::PairHashTraits<WTF::HashTraits<WebCore::StringImpl*>, WTF::HashTraits<int> >,
WTF::HashTraits<WebCore::StringImpl*> >,
std::pair<WebCore::String, WebCore::COOKIE*>>
::HashTableIteratorAdapter(const WTF::HashTableIteratorAdapter
<WTF::HashTable<WebCore::StringImpl*,
std::pair<WebCore::StringImpl*, int>,
WTF::PairFirstExtractor<std::pair<WebCore::StringImpl*, int> >,
WTF::StrHash<WebCore::StringImpl*>,
WTF::PairHashTraits<WTF::HashTraits<WebCore::StringImpl*>, WTF::HashTraits<int>>,
WTF::HashTraits<WebCore::StringImpl*> >,
std::pair<WebCore::String, WebCore::COOKIE*> >&)
ouyh12345 2007-04-13
  • 打赏
  • 举报
回复
iterator已经集成到HashMap类里了,使用时直接用begin,end等
taodm 2007-04-13
  • 打赏
  • 举报
回复
嗯?你没用stl扩展里的hash_map,用的是自己定义的?
出啥错?

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧