unordered_map的模拟实现
template<class K, class V, class HF = DefHashF<K>>
class unordered_map
{
typedef pair<K, V> ValueType;
typedef HashBucket<K, ValueType, KeyOfValue, HF> HT;
struct KeyOfValue
{
const K& operator()(const ValueType& data)
{
return data.first;
}
};
public:
typename typedef HT::Iterator iterator;
public:
unordered_map() : _ht()
{}
iterator begin()
{
return _ht.Begin();
}
iterator end()
{
return _ht.End();
}
size_t size()const
{
return _ht.Size();
}
bool empty()const
{
return _ht.Empty();
}
V& operator[](const K& key)
{
return (*(_ht.InsertUnique(ValueType(key, V())).first)).second;
}
iterator find(const K& key)
{
return _ht.Find(key);
}
size_t count(const K& key)
{
return _ht.Count(key);
}
pair<iterator, bool> insert(const ValueType& value)
{
return _ht.Insert(value);
}
iterator erase(iterator position)
{
return _ht.Erase(position);
}
size_t bucket_count()
{
return _ht.BucketCount();
}
size_t bucket_size(const K& key)
{
return _ht.BucketSize(key);
}
private:
HT _ht;
};
注意事项:
unordered_map中存储的是pair<K, V>的键值对,K为key的类型,V为value的类型,HF哈希函数类型
unordered_map在实现时,只需将hashbucket中的接口重新封装即可
来源:CSDN
作者:SU-TONG
链接:https://blog.csdn.net/weixin_44785983/article/details/104319678