遍历复杂的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;
};