• 【C++】STL详解(十二)—— 用哈希表封装出unordered_map和unordered_set


    在这里插入图片描述

    ​📝个人主页@Sherry的成长之路
    🏠学习社区:Sherry的成长之路(个人社区)
    📖专栏链接:C++学习
    🎯长路漫漫浩浩,万事皆有期待

    上一篇博客:【C++】STL详解(十一)—— unordered_set、unordered_map的介绍及使用

    哈希表源代码

    下面我们将对一个KV模型的哈希表进行封装,同时模拟实现出C++STL库当中的unordered_map和unordered_set,所用到的哈希表源代码如下:

    //每个哈希桶中存储数据的结构
    template<class K, class V>
    struct HashNode
    {
    	pair<K, V> _kv;
    	HashNode<K, V>* _next;
    
    	//构造函数
    	HashNode(const pair<K, V>& kv)
    		:_kv(kv)
    		, _next(nullptr)
    	{}
    };
    
    //哈希表
    template<class K, class V>
    class HashTable
    {
    	typedef HashNode<K, V> Node; //哈希结点类型
    public:
    	//获取本次增容后哈希表的大小
    	size_t GetNextPrime(size_t prime)
    	{
    		const int PRIMECOUNT = 28;
    		//素数序列
    		const size_t primeList[PRIMECOUNT] =
    		{
    			53ul, 97ul, 193ul, 389ul, 769ul,
    			1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
    			49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
    			1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
    			50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
    			1610612741ul, 3221225473ul, 4294967291ul
    		};
    		size_t i = 0;
    		for (i = 0; i < PRIMECOUNT; i++)
    		{
    			if (primeList[i] > prime)
    				return primeList[i];
    		}
    		return primeList[i];
    	}
    	//插入函数
    	bool Insert(const pair<K, V>& kv)
    	{
    		//1、查看哈希表中是否存在该键值的键值对
    		Node* ret = Find(kv.first);
    		if (ret) //哈希表中已经存在该键值的键值对(不允许数据冗余)
    		{
    			return false; //插入失败
    		}
    
    		//2、判断是否需要调整哈希表的大小
    		if (_n == _table.size()) //哈希表的大小为0,或负载因子超过1
    		{
    			//增容
    			//a、创建一个新的哈希表,新哈希表的大小设置为原哈希表的2倍(若哈希表大小为0,则将哈希表的初始大小设置为10)
    			vector<Node*> newtable;
    
    			newtable.resize(GetNextPrime(_table.size()));
    				
    			//b、将原哈希表当中的结点插入到新哈希表
    			for (size_t i = 0; i < _table.size(); i++)
    			{
    				if (_table[i]) //桶不为空
    				{
    					Node* cur = _table[i];
    					while (cur) //将该桶的结点取完为止
    					{
    						Node* next = cur->_next; //记录cur的下一个结点
    						size_t index = cur->_kv.first%newtable.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    						//将该结点头插到新哈希表中编号为index的哈希桶中
    						cur->_next = newtable[index];
    						newtable[index] = cur;
    
    						cur = next; //取原哈希表中该桶的下一个结点
    					}
    					_table[i] = nullptr; //该桶取完后将该桶置空
    				}
    			}
    			//c、交换这两个哈希表
    			_table.swap(newtable);
    		}
    
    		//3、将键值对插入哈希表
    		size_t index = kv.first % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    		Node* newnode = new Node(kv); //根据所给数据创建一个待插入结点
    		//将该结点头插到新哈希表中编号为index的哈希桶中
    		newnode->_next = _table[index];
    		_table[index] = newnode;
    
    		//4、哈希表中的有效元素个数加一
    		_n++;
    		return true;
    	}
    	//查找函数
    	HashNode<K, V>* Find(const K& key)
    	{
    		if (_table.size() == 0) //哈希表大小为0,查找失败
    		{
    			return nullptr;
    		}
    
    		size_t index = key % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    		//遍历编号为index的哈希桶
    		HashNode<K, V>* cur = _table[index];
    		while (cur) //直到将该桶遍历完为止
    		{
    			if (cur->_kv.first == key) //key值匹配,则查找成功
    			{
    				return cur;
    			}
    			cur = cur->_next;
    		}
    		return nullptr; //直到该桶全部遍历完毕还没有找到目标元素,查找失败
    	}
    	//删除函数
    	bool Erase(const K& key)
    	{
    		//1、通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    		size_t index = key % _table.size();
    		//2、在编号为index的哈希桶中寻找待删除结点
    		Node* prev = nullptr;
    		Node* cur = _table[index];
    		while (cur) //直到将该桶遍历完为止
    		{
    			if (cur->_kv.first == key) //key值匹配,则查找成功
    			{
    				//3、若找到了待删除结点,则删除该结点
    				if (prev == nullptr) //待删除结点是哈希桶中的第一个结点
    				{
    					_table[index] = cur->_next; //将第一个结点从该哈希桶中移除
    				}
    				else //待删除结点不是哈希桶的第一个结点
    				{
    					prev->_next = cur->_next; //将该结点从哈希桶中移除
    				}
    				delete cur; //释放该结点
    				//4、删除结点后,将哈希表中的有效元素个数减一
    				_n--;
    				return true; //删除成功
    			}
    			prev = cur;
    			cur = cur->_next;
    		}
    		//假删除可能会导致迭代器失效
    			
    		return false; //直到该桶全部遍历完毕还没有找到待删除元素,删除失败
    	}
    private:
    	vector<Node*> _table; //哈希表
    	size_t _n = 0; //哈希表中的有效元素个数
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153

    哈希表模板参数的控制

    首先需要明确的是,unordered_set是K模型的容器,而unordered_map是KV模型的容器。

    要想只用一份哈希表代码同时封装出K模型和KV模型的容器,我们必定要对哈希表的模板参数进行控制。

    为了与原哈希表的模板参数进行区分,这里将哈希表的第二个模板参数的名字改为T。

    template<class K, class T>
    class HashTable
    
    • 1
    • 2

    如果上层使用的是unordered_set容器,那么传入哈希表的模板参数就是key和key。

    template<class K>
    class unordered_set
    {
    public:
    	//...
    private:
    	HashTable<K, K> _ht; //传入底层哈希表的是K和K
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    但如果上层使用的是unordered_map容器,那么传入哈希表的模板参数就是key以及key和value构成的键值对。

    template<class K, class V>
    class unordered_map
    {
    public:
    	//...
    private:
    	HashTable<K, pair<K, V>> _ht; //传入底层哈希表的是K以及K和V构成的键值对
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    也就是说,哈希表中的模板参数T的类型到底是什么,完全却决于上层所使用容器的种类。

    而哈希结点的模板参数也应该由原来的K、V变为T:

    上层容器是unordered_set时,传入的T是键值,哈希结点中存储的就是键值。
    上层容器是unordered_map时,传入的T是键值对,哈希结点中存储的就是键值对。

    更改模板参数后,哈希结点的定义如下:

    //哈希结点的定义
    template<class T>
    struct HashNode
    {
    	T _data;
    	HashNode<T>* _next;
    
    	//构造函数
    	HashNode(const T& data)
    		:_data(data)
    		, _next(nullptr)
    	{}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在哈希映射过程中,我们需要获得元素的键值,然后通过哈希函数计算出对应的哈希地址进行映射。

    现在由于我们在哈希结点当中存储的数据类型是T,这个T可能就是一个键值,也可能是一个键值对,对于底层的哈希表来说,它并不知道哈希结点当中存储的数据究竟是什么类型,因此需要由上层容器提供一个仿函数,用于获取T类型数据当中的键值。

    因此,unordered_map容器需要向底层哈希表提供一个仿函数,该仿函数返回键值对当中的键值。

    template<class K, class V>
    class unordered_map
    {
    	//仿函数
    	struct MapKeyOfT
    	{
    		const K& operator()(const pair<K, V>& kv) //返回键值对当中的键值key
    		{
    			return kv.first;
    		}
    	};
    public:
    	//...
    private:
    	HashTable<K, pair<K, V>, MapKeyOfT> _ht;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    而虽然unordered_set容器传入哈希表的T就是键值,但是底层哈希表并不知道上层容器的种类,底层哈希表在获取键值时会统一通过传入的仿函数进行获取,因此unordered_set容器也需要向底层哈希表提供一个仿函数。

    template<class K>
    class unordered_set
    {
    	//仿函数
    	struct SetKeyOfT
    	{
    		const K& operator()(const K& key) //返回键值key
    		{
    			return key;
    		}
    	};
    public:
    	//...
    private:
    	HashTable<K, K, SetKeyOfT> _ht;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    因此,底层哈希表的模板参数现在需要增加一个,用于接收上层容器提供的仿函数。

    template<class K, class T, class KeyOfT>
    class HashTable
    
    • 1
    • 2

    string类型无法取模问题

    字符串无法取模,是哈希问题中最常见的问题。

    经过上面的分析后,我们让哈希表增加了一个模板参数,此时无论上层容器是unordered_set还是unordered_map,我们都能够通过上层容器提供的仿函数获取到元素的键值。

    但是在我们日常编写的代码中,用字符串去做键值key是非常常见的事,比如我们用unordered_map容器统计水果出现的次数时,就需要用各个水果的名字作为键值。

    而字符串并不是整型,也就意味着字符串不能直接用于计算哈希地址,我们需要通过某种方法将字符串转换成整型后,才能代入哈希函数计算哈希地址。

    但遗憾的是,我们无法找到一种能实现字符串和整型之间一对一转换的方法,因为在计算机中,整型的大小是有限的,比如用无符号整型能存储的最大数字是4294967295,而众多字符能构成的字符串的种类却是无限的。

    鉴于此,无论我们用什么方法将字符串转换成整型,都会存在哈希冲突,只是产生冲突的概率不同而已。

    经过前辈们实验后发现,BKDRHash算法无论是在实际效果还是编码实现中,效果都是最突出的。该算法由于在Brian Kernighan与Dennis Ritchie的《The C Programing Language》一书被展示而得名,是一种简单快捷的hash算法,也是Java目前采用的字符串的hash算法

    因此,现在我们需要在哈希表的模板参数中再增加一个仿函数,用于将键值key转换成对应的整型。

    template<class K, class T, class KeyOfT, class HashFunc = Hash<K>>
    class HashTable
    
    • 1
    • 2

    若是上层没有传入该仿函数,我们则使用默认的仿函数,该默认仿函数直接返回键值key即可,但是用字符串作为键值key是比较常见的,因此我们可以针对string类型写一个类模板的特化,此时当键值key为string类型时,该仿函数就会根据BKDRHash算法返回一个对应的整型。

    template<class K>
    struct Hash
    {
    	size_t operator()(const K& key) //返回键值key
    	{
    		return key;
    	}
    };
    //string类型的特化
    template<>
    struct Hash<string>
    {
    	size_t operator()(const string& s) //BKDRHash算法
    	{
    		size_t value = 0;
    		for (auto ch : s)
    		{
    			value = value * 131 + ch;
    		}
    		return value;
    	}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    哈希表默认成员函数实现

    一、构造函数
    哈希表中有两个成员变量,当我们实例化一个对象时:

    _table会自动调用vector的默认构造函数进行初始化。
    _n会根据我们所给的缺省值被设置为0。

    vector<Node*> _table; //哈希表
    size_t _n = 0; //哈希表中的有效元素个数
    
    • 1
    • 2

    因此我们不需要编写构造函数,使用默认生成的构造函数就足够了,但是由于我们后面需要编写拷贝构造函数,编写了拷贝构造函数后,默认的构造函数就不会生成了,此时我们需要使用default关键字显示指定生成默认构造函数。

    //构造函数
    HashTable() = default; //显示指定生成默认构造函数
    
    • 1
    • 2

    二、拷贝构造函数
    哈希表在拷贝时需要进行深拷贝,否则拷贝出来的哈希表和原哈希表中存储的都是同一批结点。

    哈希表的拷贝构造函数实现逻辑如下:

    将哈希表的大小调整为ht._table的大小。
    将ht._table每个桶当中的结点一个个拷贝到自己的哈希表中。
    更改哈希表当中的有效数据个数。

    //拷贝构造函数
    HashTable(const HashTable& ht)
    {
    	//1、将哈希表的大小调整为ht._table的大小
    	_table.resize(ht._table.size());
    	//2、将ht._table每个桶当中的结点一个个拷贝到自己的哈希表中(深拷贝)
    	for (size_t i = 0; i < ht._table.size(); i++)
    	{
    		if (ht._table[i]) //桶不为空
    		{
    			Node* cur = ht._table[i];
    			while (cur) //将该桶的结点取完为止
    			{
    				Node* copy = new Node(cur->_data); //创建拷贝结点
    				//将拷贝结点头插到当前桶
    				copy->_next = _table[i];
    				_table[i] = copy;
    				cur = cur->_next; //取下一个待拷贝结点
    			}
    		}
    	}
    	//3、更改哈希表当中的有效数据个数
    	_n = ht._n;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    三、赋值运算符重载函数
    实现赋值运算符重载函数时,可以通过参数间接调用拷贝构造函数,之后将拷贝构造出来的哈希表和当前哈希表的两个成员变量分别进行交换即可,当赋值运算符重载函数调用结束后,拷贝构造出来的哈希表会因为出了作用域而被自动析构,此时原哈希表之前的数据也就顺势被释放了。

    //赋值运算符重载函数
    HashTable& operator=(HashTable ht)
    {
    	//交换哈希表中两个成员变量的数据
    	_table.swap(ht._table);
    	swap(_n, ht._n);
    
    	return *this; //支持连续赋值
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    四、析构函数
    因为哈希表当中存储的结点都是new出来的,因此在哈希表被析构时必须进行结点的释放。在析构哈希表时我们只需要依次取出非空的哈希桶,遍历哈希桶当中的结点并进行释放即可。

    //析构函数
    ~HashTable()
    {
    	//将哈希表当中的结点一个个释放
    	for (size_t i = 0; i < _table.size(); i++)
    	{
    		if (_table[i]) //桶不为空
    		{
    			Node* cur = _table[i];
    			while (cur) //将该桶的结点取完为止
    			{
    				Node* next = cur->_next; //记录下一个结点
    				delete cur; //释放结点
    				cur = next;
    			}
    			_table[i] = nullptr; //将该哈希桶置空
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    哈希表正向迭代器的实现

    哈希表的正向迭代器实际上就是对哈希结点指针进行了封装,但是由于在实现++运算符重载时,可能需要在哈希表中去寻找下一个非空哈希桶,因此每一个正向迭代器中都应该存储哈希表的地址。

    //正向迭代器
    template<class K, class T, class KeyOfT, class HashFunc = Hash<K>>
    struct __HTIterator
    {
    	typedef HashNode<T> Node; //哈希结点的类型
    	typedef HashTable<K, T, KeyOfT, HashFunc> HT; //哈希表的类型
    	typedef __HTIterator<K, T, KeyOfT, HashFunc> Self; //正向迭代器的类型
    
    	Node* _node; //结点指针
    	HT* _pht; //哈希表的地址
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    因此在构造正向迭代器时,我们不仅需要对应哈希结点的指针,还需要该哈希结点所在哈希表的地址。

    //构造函数
    __HTIterator(Node* node, HT* pht)
    	:_node(node) //结点指针
    	, _pht(pht) //哈希表地址
    {}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    当对正向迭代器进行解引用操作时,我们直接返回对应结点数据的有引用即可。

    T& operator*()
    {
    	return _node->_data; //返回哈希结点中数据的引用
    }
    
    • 1
    • 2
    • 3
    • 4

    当对正向迭代器进行->操作时,我们直接返回对应结点数据的地址即可。

    T* operator->()
    {
    	return &_node->_data; //返回哈希结点中数据的地址
    }
    
    • 1
    • 2
    • 3
    • 4

    当我们需要比较两个迭代器是否相等时,只需要判断这两个迭代器所封装的结点是否是同一个即可。

    bool operator!=(const Self& s) const
    {
    	return _node != s._node; //判断两个结点的地址是否不同
    }
    
    bool operator==(const Self& s) const
    {
    	return _node == s._node; //判断两个结点的地址是否相同
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ++运算符重载函数的实现逻辑并不是很难,我们只需要知道如何找到当前结点的下一个结点即可。

    若当前结点不是当前哈希桶中的最后一个结点,则++后走到当前哈希桶的下一个结点。
    若当前结点是当前哈希桶的最后一个结点,则++后走到下一个非空哈希桶的第一个结点。

    //前置++
    Self& operator++()
    {
    	if (_node->_next) //该结点不是当前哈希桶中的最后一个结点
    	{
    		_node = _node->_next; //++后变为当前哈希桶中的下一个结点
    	}
    	else //该结点是当前哈希桶中的最后一个结点
    	{
    		KeyOfT kot;
    		HashFunc hf;
    		size_t index = hf(kot(_node->_data)) % _pht->_table.size(); //通过哈希函数计算出当前所处哈希桶编号index(除数不能是capacity)
    		index++; //从下一个位置开始找一个非空的哈希桶
    		while (index < _pht->_table.size()) //直到将整个哈希表找完
    		{
    			if (_pht->_table[index]) //当前哈希桶非空
    			{
    				_node = _pht->_table[index]; //++后变为当前哈希桶中的第一个结点
    				return *this;
    			}
    			index++; //当前哈希桶为空桶,找下一个哈希桶
    		}
    		_node = nullptr; //哈希表中已经没有空桶了,++后变为nullptr
    	}
    	return *this;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    注意: 哈希表的迭代器类型是单向迭代器,没有反向迭代器,即没有实现–运算符的重载,若是想让哈希表支持双向遍历,可以考虑将哈希桶中存储的单链表结构换为双链表结构。

    哈希表结构的其他实现方式

    在其他地方可能将插入哈希表的哈希结点统一链接到同一个单链表上,此时实现哈希表的正向迭代器时就更简单了,实现++运算符重载时,想要找到下一个结点就直接通过当前结点就可以找到。

    正向迭代器实现后,我们需要在哈希表的实现当中进行如下操作:

    进行正向迭代器类型的typedef,需要注意的是,为了让外部能够使用typedef后的正向迭代器类型iterator,我们需要在public区域进行typedef。
    由于正向迭代器中++运算符重载函数在寻找下一个结点时,会访问哈希表中的成员变量_table,而_table成员变量是哈希表的私有成员,因此我们需要将正向迭代器类声明为哈希表类的友元。
    将哈希表中查找函数返回的结点指针,改为返回由结点指针和哈希表地址构成的正向迭代器。
    将哈希表中插入函数的返回值类型,改为由正向迭代器类型和布尔类型所构成的键值对。

    然后我们就可以在哈希表中实现迭代器相关的成员函数了:

    begin函数: 返回哈希表中第一个非空哈希桶中的第一个结点的正向迭代器。
    end函数: 返回空指针的正向迭代器。

    //哈希表的实现
    template<class K, class T, class KeyOfT, class HashFunc = Hash<K>>
    class HashTable
    {
    	//将正向迭代器类声明为哈希表类的友元
    	template<class K, class T, class KeyOfT, class HashFunc>
    	friend struct __HTIterator;
    
    	typedef HashNode<T> Node; //哈希结点类型
    public:
    	typedef __HTIterator<K, T, KeyOfT, HashFunc> iterator; //正向迭代器的类型
    
    	iterator begin()
    	{
    		size_t i = 0;
    		while (i < _table.size()) //找到第一个非空哈希桶
    		{
    			if (_table[i]) //该哈希桶非空
    			{
    				return iterator(_table[i], this); //返回该哈希桶中的第一个结点的正向迭代器
    			}
    			i++;
    		}
    		return end(); //哈希桶中无数据,返回end()
    	}
    	iterator end()
    	{
    		return iterator(nullptr, this); //返回nullptr的正向迭代器
    	}
    private:
    	vector<Node*> _table; //哈希表
    	size_t _n = 0; //哈希表中的有效元素个数
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    unordered_set的模拟实现

    实现unordered_set的各个接口时,就只需要调用底层哈希表对应的接口就行了。

    template<class K>
    class unordered_set
    {
    	//仿函数
    	struct SetKeyOfT
    	{
    		const K& operator()(const K& key) //返回键值key
    		{
    			return key;
    		}
    	};
    public:
    	//现在没有实例化,没办法到HashTable里面找iterator,所以typename就是告诉编译器这里是一个类型,实例化以后再去取
    	typedef typename HashTable<K, K, SetKeyOfT>::iterator iterator;
    
    	iterator begin()
    	{
    		return _ht.begin();
    	}
    	iterator end()
    	{
    		return _ht.end();
    	}
    	//插入函数
    	pair<iterator, bool> insert(const K& key)
    	{
    		return _ht.Insert(key);
    	}
    	//删除函数
    	void erase(const K& key)
    	{
    		_ht.Erase(key);
    	}
    	//查找函数
    	iterator find(const K& key)
    	{
    		return _ht.Find(key);
    	}
    private:
    	HashTable<K, K, SetKeyOfT> _ht;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    unordered_map的模拟实现

    实现unordered_map的各个接口时,也是调用底层哈希表对应的接口就行了,此外还需要实现[ ]运算符的重载。

    template<class K, class V>
    class unordered_map
    {
    	//仿函数
    	struct MapKeyOfT
    	{
    		const K& operator()(const pair<K, V>& kv) //返回键值对当中的键值key
    		{
    			return kv.first;
    		}
    	};
    public:
    	typedef typename HashTable<K, pair<K, V>, MapKeyOfT>::iterator iterator;
    
    	iterator begin()
    	{
    		return _ht.begin();
    	}
    	iterator end()
    	{
    		return _ht.end();
    	}
    	//插入函数
    	pair<iterator, bool> insert(const pair<K, V>& kv)
    	{
    		return _ht.Insert(kv);
    	}
    	//赋值运算符重载
    	V& operator[](const K& key)
    	{
    		pair<iterator, bool> ret = insert(make_pair(key, V()));
    		iterator it = ret.first;
    		return it->second;
    	}
    	//删除函数
    	void erase(const K& key)
    	{
    		_ht.Erase(key);
    	}
    	//查找函数
    	iterator find(const K& key)
    	{
    		return _ht.Find(key);
    	}
    private:
    	HashTable<K, pair<K, V>, MapKeyOfT> _ht;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    封装完成后的代码

    哈希表的代码

    //哈希结点的定义
    template<class T>
    struct HashNode
    {
    	T _data;
    	HashNode<T>* _next;
    
    	//构造函数
    	HashNode(const T& data)
    		:_data(data)
    		, _next(nullptr)
    	{}
    };
    template<class K>
    struct Hash
    {
    	size_t operator()(const K& key) //返回键值key
    	{
    		return key;
    	}
    };
    //string类型的特化
    template<>
    struct Hash<string>
    {
    	size_t operator()(const string& s) //BKDRHash算法
    	{
    		size_t value = 0;
    		for (auto ch : s)
    		{
    			value = value * 131 + ch;
    		}
    		return value;
    	}
    };
    //哈希表的实现
    template<class K, class T, class KeyOfT, class HashFunc = Hash<K>>
    class HashTable
    {
    	//将正向迭代器类声明为哈希表类的友元
    	template<class K, class T, class KeyOfT, class HashFunc>
    	friend struct __HTIterator;
    	//friend struct __HTIterator;
    	typedef HashNode<T> Node; //哈希结点类型
    public:
    	typedef __HTIterator<K, T, KeyOfT, HashFunc> iterator; //正向迭代器的类型
    
    	iterator begin()
    	{
    		size_t i = 0;
    		while (i < _table.size()) //找到第一个非空哈希桶
    		{
    			if (_table[i]) //该哈希桶非空
    			{
    				return iterator(_table[i], this); //返回该哈希桶中的第一个结点的正向迭代器
    			}
    			i++;
    		}
    		return end(); //哈希桶中无数据,返回end()
    	}
    	iterator end()
    	{
    		return iterator(nullptr, this); //返回nullptr的正向迭代器
    	}
    
    	//构造函数
    	HashTable() = default; //显示指定生成默认构造
    
    	//拷贝构造函数
    	HashTable(const HashTable& ht)
    	{
    		//1、将哈希表的大小调整为ht._table的大小
    		_table.resize(ht._table.size());
    		//2、将ht._table每个桶当中的结点一个个拷贝到自己的哈希表中(深拷贝)
    		for (size_t i = 0; i < ht._table.size(); i++)
    		{
    			if (ht._table[i]) //桶不为空
    			{
    				Node* cur = ht._table[i];
    				while (cur) //将该桶的结点取完为止
    				{
    					Node* copy = new Node(cur->_data); //创建拷贝结点
    					//将拷贝结点头插到当前桶
    					copy->_next = _table[i];
    					_table[i] = copy;
    					cur = cur->_next; //取下一个待拷贝结点
    				}
    			}
    		}
    		//3、更改哈希表当中的有效数据个数
    		_n = ht._n;
    	}
    	//赋值运算符重载函数
    	HashTable& operator=(HashTable ht)
    	{
    		//交换哈希表中两个成员变量的数据
    		_table.swap(ht._table);
    		swap(_n, ht._n);
    
    		return *this; //支持连续赋值
    	}
    	//析构函数
    	~HashTable()
    	{
    		//将哈希表当中的结点一个个释放
    		for (size_t i = 0; i < _table.size(); i++)
    		{
    			if (_table[i]) //桶不为空
    			{
    				Node* cur = _table[i];
    				while (cur) //将该桶的结点取完为止
    				{
    					Node* next = cur->_next; //记录下一个结点
    					delete cur; //释放结点
    					cur = next;
    				}
    				_table[i] = nullptr; //将该哈希桶置空
    			}
    		}
    	}
    	//获取本次增容后哈希表的大小
    	size_t GetNextPrime(size_t prime)
    	{
    		const int PRIMECOUNT = 28;
    		//素数序列
    		const size_t primeList[PRIMECOUNT] =
    		{
    			53ul, 97ul, 193ul, 389ul, 769ul,
    			1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
    			49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
    			1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
    			50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
    			1610612741ul, 3221225473ul, 4294967291ul
    		};
    		size_t i = 0;
    		for (i = 0; i < PRIMECOUNT; i++)
    		{
    			if (primeList[i] > prime)
    				return primeList[i];
    		}
    		return primeList[i];
    	}
    	//插入函数
    	pair<iterator, bool> Insert(const T& data)
    	{
    		KeyOfT kot;
    		//1、查看哈希表中是否存在该键值的键值对
    		iterator ret = Find(kot(data));
    		if (ret != end()) //哈希表中已经存在该键值的键值对(不允许数据冗余)
    		{
    			return make_pair(ret, false); //插入失败
    		}
    
    		//2、判断是否需要调整哈希表的大小
    		if (_n == _table.size()) //哈希表的大小为0,或负载因子超过1
    		{
    			//增容
    			//a、创建一个新的哈希表,新哈希表的大小设置为原哈希表的2倍(若哈希表大小为0,则将哈希表的初始大小设置为10)
    			HashFunc hf;
    			vector<Node*> newtable;
    			//size_t newsize = _table.size() == 0 ? 10 : _table.size() * 2;
    			//newtable.resize(newsize);
    
    			newtable.resize(GetNextPrime(_table.size()));
    				
    			//b、将原哈希表当中的结点插入到新哈希表
    			for (size_t i = 0; i < _table.size(); i++)
    			{
    				if (_table[i]) //桶不为空
    				{
    					Node* cur = _table[i];
    					while (cur) //将该桶的结点取完为止
    					{
    						Node* next = cur->_next; //记录cur的下一个结点
    						size_t index = hf(kot(cur->_data))%newtable.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    						//将该结点头插到新哈希表中编号为index的哈希桶中
    						cur->_next = newtable[index];
    						newtable[index] = cur;
    
    						cur = next; //取原哈希表中该桶的下一个结点
    					}
    					_table[i] = nullptr; //该桶取完后将该桶置空
    				}
    			}
    			//c、交换这两个哈希表
    			_table.swap(newtable);
    		}
    
    		//3、将键值对插入哈希表
    		HashFunc hf;
    		size_t index = hf(kot(data)) % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    		Node* newnode = new Node(data); //根据所给数据创建一个待插入结点
    		//将该结点头插到新哈希表中编号为index的哈希桶中
    		newnode->_next = _table[index];
    		_table[index] = newnode;
    
    		//4、哈希表中的有效元素个数加一
    		_n++;
    		return make_pair(iterator(newnode, this), true);
    	}
    	//查找函数
    	iterator Find(const K& key)
    	{
    		if (_table.size() == 0) //哈希表大小为0,查找失败
    		{
    			return end();
    		}
    
    		KeyOfT kot;
    		HashFunc hf;
    		size_t index = hf(key) % _table.size(); //通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    		//遍历编号为index的哈希桶
    		HashNode<T>* cur = _table[index];
    		while (cur) //直到将该桶遍历完为止
    		{
    			if (kot(cur->_data) == key) //key值匹配,则查找成功
    			{
    				return iterator(cur, this);
    			}
    			cur = cur->_next;
    		}
    		return end(); //直到该桶全部遍历完毕还没有找到目标元素,查找失败
    	}
    	//删除函数
    	bool Erase(const K& key)
    	{
    		KeyOfT kot;
    		HashFunc hf;
    		//1、通过哈希函数计算出对应的哈希桶编号index(除数不能是capacity)
    		size_t index = hf(key) % _table.size();
    		//2、在编号为index的哈希桶中寻找待删除结点
    		Node* prev = nullptr;
    		Node* cur = _table[index];
    		while (cur) //直到将该桶遍历完为止
    		{
    			if (kot(cur->_data) == key) //key值匹配,则查找成功
    			{
    				//3、若找到了待删除结点,则删除该结点
    				if (prev == nullptr) //待删除结点是哈希桶中的第一个结点
    				{
    					_table[index] = cur->_next; //将第一个结点从该哈希桶中移除
    				}
    				else //待删除结点不是哈希桶的第一个结点
    				{
    					prev->_next = cur->_next; //将该结点从哈希桶中移除
    				}
    				delete cur; //释放该结点
    				//4、删除结点后,将哈希表中的有效元素个数减一
    				_n--;
    				return true; //删除成功
    			}
    			prev = cur;
    			cur = cur->_next;
    		}
    		//假删除可能会导致迭代器失效
    			
    		return false; //直到该桶全部遍历完毕还没有找到待删除元素,删除失败
    	}
    private:
    	vector<Node*> _table; //哈希表
    	size_t _n = 0; //哈希表中的有效元素个数
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262

    正向迭代器的代码

    //前置声明
    template<class K, class T, class KeyOfT, class HashFunc>
    class HashTable;
    
    //正向迭代器
    template<class K, class T, class KeyOfT, class HashFunc = Hash<K>>
    struct __HTIterator
    {
    	typedef HashNode<T> Node; //哈希结点的类型
    	typedef HashTable<K, T, KeyOfT, HashFunc> HT; //哈希表的类型
    	typedef __HTIterator<K, T, KeyOfT, HashFunc> Self; //正向迭代器的类型
    
    	Node* _node; //结点指针
    	HT* _pht; //哈希表的地址
    
    	//构造函数
    	__HTIterator(Node* node, HT* pht)
    		:_node(node) //结点指针
    		, _pht(pht) //哈希表地址
    	{}
    
    	T& operator*()
    	{
    		return _node->_data; //返回哈希结点中数据的引用
    	}
    
    	T* operator->()
    	{
    		return &_node->_data; //返回哈希结点中数据的地址
    	}
    
    	bool operator!=(const Self& s) const
    	{
    		return _node != s._node; //判断两个结点的地址是否不同
    	}
    
    	bool operator==(const Self& s) const
    	{
    		return _node == s._node; //判断两个结点的地址是否相同
    	}
    
    	//前置++
    	Self& operator++()
    	{
    		if (_node->_next) //该结点不是当前哈希桶中的最后一个结点
    		{
    			_node = _node->_next; //++后变为当前哈希桶中的下一个结点
    		}
    		else //该结点是当前哈希桶中的最后一个结点
    		{
    			KeyOfT kot;
    			HashFunc hf;
    			size_t index = hf(kot(_node->_data)) % _pht->_table.size(); //通过哈希函数计算出当前所处哈希桶编号index(除数不能是capacity)
    			index++; //从下一个位置开始找一个非空的哈希桶
    			while (index < _pht->_table.size()) //直到将整个哈希表找完
    			{
    				if (_pht->_table[index]) //当前哈希桶非空
    				{
    					_node = _pht->_table[index]; //++后变为当前哈希桶中的第一个结点
    					return *this;
    				}
    				index++; //当前哈希桶为空桶,找下一个哈希桶
    			}
    			_node = nullptr; //哈希表中已经没有空桶了,++后变为nullptr
    		}
    		return *this;
    	}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    unordered_set的代码

    namespace sherry //防止命名冲突
    {
    	template<class K>
    	class unordered_set
    	{
    		//仿函数
    		struct SetKeyOfT
    		{
    			const K& operator()(const K& key) //返回键值key
    			{
    				return key;
    			}
    		};
    	public:
    		//现在没有实例化,没办法到HashTable里面找iterator,所以typename就是告诉编译器这里是一个类型,实例化以后再去取
    		typedef typename HashTable<K, K, SetKeyOfT>::iterator iterator;
    
    		iterator begin()
    		{
    			return _ht.begin();
    		}
    		iterator end()
    		{
    			return _ht.end();
    		}
    		//插入函数
    		pair<iterator, bool> insert(const K& key)
    		{
    			return _ht.Insert(key);
    		}
    		//删除函数
    		void erase(const K& key)
    		{
    			_ht.Erase(key);
    		}
    		//查找函数
    		iterator find(const K& key)
    		{
    			return _ht.Find(key);
    		}
    	private:
    		HashTable<K, K, SetKeyOfT> _ht;
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    unordered_map的代码

    namespace sherry //防止命名冲突
    {
    	template<class K, class V>
    	class unordered_map
    	{
    		//仿函数
    		struct MapKeyOfT
    		{
    			const K& operator()(const pair<K, V>& kv) //返回键值对当中的键值key
    			{
    				return kv.first;
    			}
    		};
    	public:
    		typedef typename HashTable<K, pair<K, V>, MapKeyOfT>::iterator iterator;
    
    		iterator begin()
    		{
    			return _ht.begin();
    		}
    		iterator end()
    		{
    			return _ht.end();
    		}
    		//插入函数
    		pair<iterator, bool> insert(const pair<K, V>& kv)
    		{
    			return _ht.Insert(kv);
    		}
    		//赋值运算符重载
    		V& operator[](const K& key)
    		{
    			pair<iterator, bool> ret = insert(make_pair(key, V()));
    			iterator it = ret.first;
    			return it->second;
    		}
    		//删除函数
    		void erase(const K& key)
    		{
    			_ht.Erase(key);
    		}
    		//查找函数
    		iterator find(const K& key)
    		{
    			return _ht.Find(key);
    		}
    	private:
    		HashTable<K, pair<K, V>, MapKeyOfT> _ht;
    	};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    总结:

    今天我们比较详细地完成了用一个哈希表同时封装出unordered_map和unordered_set,了解了一些有关的底层原理。接下来,我们将进行STL中bitset类的学习。希望我的文章和讲解能对大家的学习提供一些帮助。

    当然,本文仍有许多不足之处,欢迎各位小伙伴们随时私信交流、批评指正!我们下期见~

    在这里插入图片描述

  • 相关阅读:
    Vim - Linux环境基础开发工具使用
    软件测试(基础知识)
    【完美世界】石昊挑逗云曦,斩杀神级猿魔,吃血魂草开新挂,团灭战族追兵
    Scratch二次开发8:背景、角色、造型、声音后台管理
    前端Mock神器-Apifox
    面试官:同学,冒泡太简单了,要不手写一个【快速排序】吧...
    Zookeeper集群 + Kafka集群
    时序分解 | Matlab实现EEMD集合经验模态分解时间序列信号分解
    简述树状数组
    Promise 一: 基本问题
  • 原文地址:https://blog.csdn.net/m0_73258399/article/details/133560583