当前位置: 首页 > news >正文

哈希表特性与unordered_map/unordered_set实现分析

目录

一、哈希表核心特性总结

1.开放地址法

2.链地址法

二、unordered_map/unordered_set实现要点分析

1. 哈希表核心实现(HashTable2.h)

(1) 哈希函数处理

(2) 链地址法实现

(3) 迭代器设计

(4) hashtable设计

2. unordered_map实现要点

3. unordered_map实现要点


一、哈希表核心特性总结

哈希表有两种表 : 一种是闭散列(开放地址法),一种是开散列(链地址法),我将用画图来带大家理解这两种方法的思路

1.开放地址法

线性探测

v

2.链地址法

二、unordered_map/unordered_set实现要点分析

1. 哈希表核心实现(HashTable2.h)

(1) 哈希函数处理

仅使用首字符会导致大量冲突(如所有以相同字母开头的字符串),使用BKDR哈希,通过累乘质数和字符值获得更好分布

// 默认哈希函数(直接类型转换)
template<class K>
struct DefaultHashFunc {size_t operator()(const K& key) {return (size_t)key;}
};// 字符串特化版本
template<>
struct DefaultHashFunc<string> {size_t operator()(const string& str) {size_t hash = 0;for(auto ch : str) {hash += 131;hash += ch;}return hash;}
};
(2) 链地址法实现
template<class T>
struct HashNode
{T _data;HashNode<T>* next;HashNode(const T& data):_data(data), next(nullptr){}
};
(3) 迭代器设计
//前置申明,告诉Iterator,申明了哈希表
template<class K, class T, class KeyOfT, class HashFunc >
class HashTable;template<class K, class T,class Ptr,class Ref, class KeyOfT, class HashFunc>
struct HTIterator
{typedef HashNode<T> Node; typedef HTIterator<K, T,Ptr,Ref, KeyOfT, HashFunc>  Self;//这是什么鬼??typedef HTIterator<K, T, T*, T&, KeyOfT, HashFunc>  Iterator;Node* _node;//就是不能改*phtconst HashTable<K, T, KeyOfT, HashFunc>* _pht;//为什么需要节点的指针和哈希的指针/*HTIterator(Node * node,HashTable<K, T, KeyOfT, HashFunc>* pht):_node(node),_pht(pht){}*///这个_pht加了const的重载HTIterator(Node* node,const  HashTable<K, T, KeyOfT, HashFunc>* pht):_node(node), _pht(pht){}//普通迭代器时,它是拷贝构造//const迭代器时,它是构造HTIterator(const Iterator & it):_node(it._node), _pht(it._pht){}Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}Self& operator++(){if (_node->next){//当前桶还没完_node = _node->next;}else{KeyOfT kot;HashFunc hf;size_t hashi = hf(kot(_node->_data)) % _pht->_table.size();//从下一个位置,查找不为空的桶++hashi;while (hashi < _pht->_table.size()){if (_pht->_table[hashi]){//不为空就退出_node = _pht->_table[hashi];return (*this);}else{//为空继续加 ++hashi;}}_node = nullptr;}return *this;}bool operator!=(const Self& s){return  _node != s._node;}bool operator==(const Self& s){return  _node == s._node;}
};
(4) hashtable设计
	template<class K, class T,class KeyOfT, class HashFunc = DefaultHashFunc<K>>class HashTable{typedef HashNode<T> Node;////友元声明,类模版需要把模版参数带上template<class K, class T,class Ptr,class Ref ,class KeyOfT, class HashFunc >friend struct HTIterator;		public:typedef HTIterator<K, T,T*,T&, KeyOfT, HashFunc>  iterator;typedef HTIterator<K, T,const T*,const  T&, KeyOfT, HashFunc>  const_iterator;iterator begin(){//找第一个桶for (size_t i =0 ;i < _table.size();i++){Node* cur = _table[i];if (cur){//这里为什么传this ???  航哥说这里this是哈希表的指针return iterator(cur, this);}}//没有找到return iterator(nullptr, this);}iterator end(){return iterator(nullptr,this);}const_iterator begin()const{//找第一个桶for (size_t i = 0;i < _table.size();i++){Node* cur = _table[i];if (cur){return const_iterator(cur, this);}}//没有找到return const_iterator(nullptr, this);}const_iterator end()const{return const_iterator(nullptr, this);}HashTable(){//先把size开到10,然后把剩余的位置另存为空指针_table.resize(10, nullptr);}~HashTable(){for (size_t i = 0;i < _table.size();i++){Node* cur = _table[i];while (cur){Node* next = cur->next;delete cur;//	free cur;cur = next;}//因为cur是野指针,如果不置空,那么他有可能还会指向原来的节点cur = nullptr;}}pair<iterator,bool> insert(const T& data){KeyOfT kot;HashFunc hf;iterator it = Find(kot(data));//在这里是证明有相同的内容if (it!=end()){return make_pair(it,false);}//负载因子到一就扩容if (_n == _table.size()){size_t newSize = _table.size() * 2;//创建新表HashTable<K,T,KeyOfT,HashFunc> newht;//这个需要开新节点,而且销毁也麻烦//for (size_t i = 0;i < _table.size();i++)//{//	//.......//	ht.insert();//}vector<Node*> newTable;newTable.resize(newSize, nullptr);//便利旧表,顺手牵羊,把节点签下来挂到新表for (size_t i = 0;i < _table.size();i++){Node* cur = _table[i];while (cur){Node* next = cur->next;//头插新表size_t hashi = hf(kot(cur->_data)) % newSize;cur->next = newTable[hashi];newTable[hashi] = cur;cur = next;}_table[i] = nullptr;}_table.swap(newTable);}size_t hashi = hf(kot(data)) % _table.size();//头插,这个没看懂Node* newnode = new Node(data);newnode->next = _table[hashi];_table[hashi] = newnode;++_n;return make_pair(iterator(newnode,this), true);}iterator Find(const K& key){HashFunc hf;KeyOfT kot;size_t hashi = hf(key) % _table.size();Node* cur = _table[hashi];while (cur){if (kot(cur->_data) == key){return iterator(cur,this);}cur = cur->next;}return iterator(nullptr,this);}void Print(){for (size_t i = 0;i < _table.size();i++){printf("[%d]->", i);Node* cur = _table[i];while (cur){cout << cur->_kv.first << "->" << cur->_kv.second << "->";cur = cur->next;}printf("NULL\n");}}bool Erase(const K& key){HashFunc hf;KeyOfT kot;size_t hashi = hf(key) % _table.size();Node* cur = _table[hashi];Node* prev = nullptr;while (cur){if (kot(cur->_data) == key){if (prev == nullptr){_table[hashi] = cur->next;}else{prev->next = cur->next;}delete cur;return true;}prev = cur;cur = cur->next;}--_n;return false;}private:vector<Node*> _table;//指针数组size_t _n = 0;};

2. unordered_map实现要点

template<class K,class V>
class  unordered_map
{struct MapKeyOfT{const K& operator()(const pair<K,V>& kv){return kv.first;}};
public:typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT>::iterator  iterator;typedef typename hash_bucket::HashTable<K, pair<const K, V>, MapKeyOfT>::const_iterator  const_iterator;pair<iterator,bool> insert(const pair<K,V>& kv){return _ht.insert(kv);}iterator begin(){return _ht.begin();}iterator end(){return _ht.end();}const_iterator begin()const{return _ht.begin();}const_iterator end()const{return _ht.end();}V& operator[](const K& key){pair<iterator,bool> ret=_ht.insert(make_pair(key,V()));return ret.first->second;}
private://这种const的特殊属性,一般是自己设置hash_bucket::HashTable<K,pair<const K,V>,MapKeyOfT >  _ht;
};

3. unordered_map实现要点

template<class K>
class unordered_set
{struct SetKeyOfT{const K & operator()(const K & key){return key;}};public:typedef	typename hash_bucket::HashTable<K,K,SetKeyOfT>::const_iterator iterator;typedef	typename hash_bucket::HashTable<K, K, SetKeyOfT>::const_iterator const_iterator;iterator begin()const
{return _ht.begin();
}iterator end()const
{return _ht.end();
}
pair<iterator,bool> insert(const K& key)
{//这样写是错的,因为这里接受的是const_iterator,返回的是iteratorpair<hash_bucket::HashTable<K, K, SetKeyOfT>::iterator, bool> ret = _ht.insert(key);return make_pair(ret.first, ret.second);
}private:hash_bucket::HashTable<K, K,SetKeyOfT>  _ht;
};

http://www.lryc.cn/news/620484.html

相关文章:

  • 【159页PPT】智慧方案企业数字化转型流程体系建设与运营方案(附下载方式)
  • 群晖 NAS 影音访问:通过 cpolar 内网穿透服务实现 Nastool 远程管理
  • openvsx搭建私有插件仓库
  • Elasticsearch RBAC 配置:打造多租户环境的安全访问控制
  • Cherryusb UAC例程对接STM32 SAI播放音乐和录音(上)=>SAI+TX+RX+DMA的配置与音频回环测试
  • 深入详解C语言数组:承上启下——从C语言数组基础到数据结构衔接
  • 抓取系统升级,是优化还是重构更合适?
  • CSS aspect-ratio 属性
  • RTC时钟倒计时数码管同步显示实现(STC8)
  • 【基于个人博客系统】---测试报告
  • 当GitHub宕机时,我们如何协作?
  • GO学习记录五——数据库表的增删改查
  • 手写MyBatis第16弹:泛型魔法应用:MyBatis如何破解List的运行时类型
  • C++ 应用场景全景解析:从系统级到AI的跨越式演进
  • 分布式系统架构设计模式:从微服务到云原生
  • 河南萌新联赛2025第(五)场:信息工程大学”(补题)
  • DataHub OPC Gateway:实现OPC UA与OPC DA无缝集成的高性能网关
  • iOS App TF上架全流程实战 高效内测分发与IPA包管理
  • Boost库中Pool 基础内存池(boost::pool<>)的详细用法解析和实战应用
  • Docker 核心技术:Namespace
  • 版本更新!FairGuard-Mac加固工具已上线!
  • 银河麒麟系统部署oceanbase社区版
  • 【入门级-C++程序设计:13、STL 模板:栈(stack)、队 列(queue)、 链 表(list)、 向 量(vector) 等容器】
  • 中介者模式和观察者模式的区别是什么
  • mysql——count(*)、count(1)和count(字段)谁更快?有什么区别?
  • 【React】hooks 中的闭包陷阱
  • 某处卖600的【独角仙】尾盘十分钟短线 尾盘短线思路 手机电脑通用无未来函数
  • coze小白-如何用coze上传本地文件?(对话流使用)
  • 《SeeClick: Harnessing GUI Grounding for Advanced Visual GUI Agents》论文精读笔记
  • 云原生俱乐部-k8s知识点归纳(1)