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