templateclass Pair { private: HashedObj key; Object def; public: explicit Pair(const HashedObj& k = HashedObj{}, const Object& de = Object{}) : key(k), def(de) {} const HashedObj& first() const { return key; } const Object& second() const { return def; } bool operator == (const Pair& rhs) const { return key == rhs.key; } bool operator != (const Pair& rhs) const { return !(*this == rhs); } }; template class Hash { public: size_t operator()(const Key& key) const { } }; template <> class Hash > { public: size_t operator() (const Pair & rhs) const { static unordered_set ::hasher hf; return hf(rhs.first()); } }; template class Dictionary { private: unordered_set , Hash > > items; public: Dictionary() { } void insert(const HashedObj& k, const Object& definition) { items.insert(Pair (k, definition)); } const Object& lookup(const HashedObj& key) const { auto iter = items.find(Pair (key)); return (*iter).second(); } bool isEmpty()const { return items.empty(); } void makeEmpty() { items.clear(); } };
示例
Dictionarydic; for (int i = 0; i < 1000; i++) { dic.insert(to_string(i), i + 1); } cout << dic.lookup(to_string(888)) << endl;



