文章目錄
- (一)認識unordered_map和unordered_set
- (二)模擬實現unordered_map和unordered_set
- 2.1 實現出復用哈希表的框架
- 2.2 迭代器iterator的實現思路分析
- 2.3 unordered_map支持[]
- (三)結束語
(一)認識unordered_map和unordered_set
unordered_map和unordered_set這兩個容器是C++11之后才更新的。unordered_map的底層復用了哈希表來實現key/value的結構,而unordered_set的底層也是復用了哈希表,但實現的是key的結構。這兩個容器的使用其實跟map和set的使用是一致的,只是map和set的底層復用的是紅黑樹,unordered_set和unordered_map這兩個容器的使用如下代碼:
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>using namespace std;int main()
{//unordered_mapunordered_map<int, int> myunmap = { {4,4},{2,2},{8,8},{11,11},{2,2} };for (auto& e : myunmap){cout << e.first << ":" << e.second << endl;}//unordered_setunordered_set<int> myunset = { 3,4,2,1,7,0,5,6,9,8,3,3 };for (auto& e : myunset){cout << e << " ";}cout << endl << endl;//mapmap<int, int> mymap = { {4,4},{2,2},{8,8},{11,11},{2,2} };for (auto& e : mymap){cout << e.first << ":" << e.second << endl;}//setset<int> myset = { 3,4,2,1,7,0,5,6,9,8,3,3 };for (auto& e : myset){cout << e << " ";}cout << endl;return 0;
}
打印結果:
從代碼和打印結果來看,unordered_map和unordered_set這兩個容器跟map、set一樣都支持initializer_list初始化,但是從打印結果來看,unordered_map和unordered_set支持去重+不排序,而map和set支持去重+排序,這就是它們底層復用的封裝不同所導致的,map和set的底層是紅黑樹,迭代器在遍歷時走中序遍歷,所以打印出來的數據是有序的,而unordered_map和unordered_set底層是哈希表,哈希表是通過哈希桶來將值一個個存儲進去的,迭代器遍歷時是遍歷一個個哈希桶,所以底層復用的不同,實現出來的效果也略有不同,但是這幾個容器的使用方式完全是類似的。
(二)模擬實現unordered_map和unordered_set
2.1 實現出復用哈希表的框架
- 首先我們得知道,哈希表既能被unordered_map復用又能被unordered_set復用,而unordered_map的數據結構是pair<key,value>,而unordered_set的數據結構就是一個key,由于這兩個容器的存儲的數據結構不同,所以哈希表在實現時應該使用泛型參數T來接收unordered_map和unordered_set傳過來的數據結構的類型,才能實例化出不同的哈希表,提供給這兩個容器使用
- 哈希表在實現時還需要使用一個K參數來接收unordered_map和unordered_set的關鍵字,因為這兩個容器的find/erase等一些接口都是通過關鍵字來實現的。unordered_set傳給泛型T的就是關鍵字,但還是需要再傳一遍,就是為了要與unordered_map保持兼容,unordered_map傳給泛型T的是一堆pair值,實現find/erase等接口時就不方便,所以為了保持兼容,哈希表在實現時還需要使用一個K參數來接收unordered_map和unordered_set的關鍵字
- 因為哈希表實現了泛型T,不知道傳過來的數據是k,還是pair<k,v>,那么insert內部進行插入時要用k對象轉換成整型取模和比較相等,因為pair的value不參與計算取模,且默認支持的是key和value一起比較相等,所以在任何時候只需要比較k對象,那么我們就需要在unordered_map和unordered_set層分別實現一個MapOfKey和SetOfKey的仿函數來傳給哈希表中的KeyOfT,然后在哈希表中通過KeyOfT仿函數取出T類型對象中的k對象,再轉換成整型取模和k比較相等
代碼實現如下:
步驟1:實現哈希表
#pragma once
#include <iostream>
#include <vector>
using namespace std;//將關鍵字轉成可以取模,如string本身取不了模,利用ASCII碼值即可,用仿函數實現
template<class K>
class HashFunc
{
public://全部轉成無符號的整型,這樣才能取模const size_t operator()(const K& key){return (size_t)key;}
};
//使用庫里面的unordered_map和unordered_set時,我們不用給string的取模進行仿函數的實現,因為庫里面為string的仿函數進行了特化
template<>
class HashFunc<string>
{
public:size_t operator()(const string& str){size_t ret = 0;for (auto& s : str){ret += s;ret *= 131;}return ret;}
};namespace li //模擬實現時防止與庫里面的沖突,用了命名空間
{template<class T>struct HashNode{T _data;HashNode<T>* _next;HashNode(const T& data):_data(data),_next(nullptr){ }};template<class K,class T,class KeyOfT,class Hash>class HashTable{typedef HashNode<T> Node;public:inline unsigned long _stl_next_prime(unsigned long n){static const int _stl_num_primes = 28;static const unsigned long _stl_primes_list[_stl_num_primes] ={53, 97, 193, 389, 769,1543, 3079, 6151, 12289, 24593,49157, 98317, 196613, 393241, 786433,1572869, 3145739, 6291469, 12582917, 25165843,50331653, 100663319, 201326611, 402653189, 805306457,1610612741, 3221225473, 4294967291};const unsigned long* first = _stl_primes_list;const unsigned long* last = _stl_primes_list + _stl_num_primes;const unsigned long* pos = lower_bound(first, last, n);//lower_bound()在迭代器的范圍內取>=n的最小值return pos == last ? *(pos - 1) : *(pos);}HashTable(){//提前開好第一個質數大小的空間_tables.resize(_stl_next_prime(1),nullptr);}~HashTable(){for (int i = 0; i < _tables.size(); i++){Node* cur = _tables[i];while (cur){Node* next = cur->_next;delete cur;cur = next;}_tables[i] = nullptr;}}bool insert(const T& data){Hash hs;KeyOfT kot;if(find(kot(data)))return false;//負載因子為1就擴容if (_n == _tables.size()){unsigned long newsize = _stl_next_prime(_tables.size() + 1);vector<Node*> newtables(newsize, nullptr);for (int i = 0; i < _tables.size(); i++){//遍歷舊表,將數據挪到新表Node* cur = _tables[i];while (cur){Node* next = cur->_next;size_t hashi = hs(kot(cur->_data)) % newtables.size();cur->_next = newtables[hashi];newtables[hashi] = cur;cur = next;}_tables[i] = nullptr;}_tables.swap(newtables);}//算出key在哈希表中映射的存儲空間size_t hashi = hs(kot(data)) % _tables.size();//頭插Node* cur = new Node(data);cur->_next = _tables[hashi];_tables[hashi] = cur;++_n;return true;}bool find(const K& key){Hash hs;KeyOfT kot;size_t hashi = hs(key) % _tables.size();Node* cur = _tables[hashi];while (cur){if (hs(kot(cur->_data)) == hs(key)){return true;}cur = cur->_next;}return false;}bool erase(const K& key){Hash hs;KeyOfT kot;size_t hashi = hs(key) % _tables.size();Node* cur = _tables[hashi];Node* prev = nullptr;while (cur){if (hs(kot(cur->_data)) == hs(key)){if (prev == nullptr){_tables[hashi] = cur->_next;}else{prev->_next = cur->_next;}delete cur;--_n;return true;}prev = cur;cur = cur->_next;}return false;}private:vector<Node*> _tables;size_t _n = 0;};
}
步驟2:封裝unordered_map和unordered_set的框架,解決獲取關鍵字(KeyOfT)的問題
//unordered_map.h
#pragma once
#include "HashTable.h"
namespace Unordered_map
{template<class K, class V,class Hash = HashFunc<K>>class unordered_map{struct MapOfKey{const K& operator()(const pair<K, V>& kv){return kv.first;}};public:bool insert(const pair<K, V>& kv){return _hash.insert(kv);}bool find(const K& key){return _hash.find(key);}bool erase(const K& key){return _hash.erase(key);}private:li::HashTable<K, pair<const K, V>,MapOfKey,Hash> _hash;};
}
//unordered_set.h
#pragma once
#include "HashTable.h"
namespace Unordered_set
{template<class K,class Hash = HashFunc<K>>class unordered_set{struct SetOfKey{const K& operator()(const K& key){return key;}};public:bool insert(const K& key){return _hash.insert(key);}bool find(const K& key){return _hash.find(key);}bool erase(const K& key){return _hash.erase(key);}private:li::HashTable<K,const K,SetOfKey,Hash> _hash;};
}
2.2 迭代器iterator的實現思路分析
- iterator的實現就是用一個類型去封裝結點的哈希結點的指針,再通過重載運算符實現迭代器像指針一樣訪問的行為,這里哈希表的迭代器是一個單項迭代器
- 重載運算符中,operator++如何實現呢,iterator中有一個結點的指針,該指針指向哈希桶里的一個結點,若桶下面還有結點,則將迭代器指向下一個結點即可。若桶的下面沒有結點,則需要找到哈希桶,為了能找到下一個桶,我們需要在迭代器中增加一個哈希表指針,讓該指針指向哈希表,這樣的話若當前桶走完了,要找到下一個桶就方便了,直接用key值計算出當前桶的位置,依次往后找不為空的桶即可,若往后找到的桶都為空,遍歷到哈希尾,就可返回一個nullptr
代碼如下:
步驟1:實現哈希表的迭代器,重載運算符
template<class K, class T, class KeyOfT, class Hash>
class HashTable;template<class K, class T, class KeyOfT, class Hash>
struct HashTableIterator
{typedef HashNode<T> Node;typedef HashTable<K, T, KeyOfT, Hash> ht;typedef HashTableIterator<K, T, KeyOfT, Hash> Self;Node* _node; //結點的指針const ht* _ht; //哈希表指針HashTableIterator(Node* node,const ht* ht):_node(node),_ht(ht){ }T& operator*(){return _node->_data;}T* operator->(){return &_node->_data;}Self operator++(){if (_node->_next){//說明桶的下面還有結點_node = _node->_next;}else{KeyOfT kot;Hash hs;size_t hashi = hs(kot(_node->_data)) % _ht->_tables.size();hashi++;//找下一個不為空的桶while (hashi < _ht->_tables.size()){if (_ht->_tables[hashi]){_node = _ht->_tables[hashi];break;}else{hashi++;}}if (hashi == _ht->_tables.size()){//沒有不為空的桶_node = nullptr;}}return *this;}bool operator!=(const Self& s){return _node != s._node;}bool operator==(const Self& s){return _node == s._node;}
};
步驟2:begin()返回第一個桶中第一個結點指針構造的迭代器,end()返回的迭代器可以用空來表示
template<class K,class T,class KeyOfT,class Hash>
class HashTable
{typedef HashNode<T> Node;template<class K, class T, class KeyOfT, class Hash>friend struct HashTableIterator; //友元聲明,方便迭代器訪問哈希表中的私有成員public:typedef HashTableIterator<K, T, KeyOfT, Hash> Iterator;Iterator Begin(){for (int i = 0; i < _tables.size(); i++){Node* cur = _tables[i];if (cur){return Iterator(cur, this);}}return End();}Iterator End(){return Iterator(nullptr, this);}
};
上面兩個步驟實現的迭代器是可以修改的,我們知道unordered_map和unordered_set的關鍵字是不可以被修改的,所以我們需要把unordered_set的第二個模板參數改成const K,unordered_map的第二個模板參數改成pair<const K,V>,代碼如下:
步驟3:封裝哈希迭代器實現unordered_map和unordered_set的迭代器
//unordered_map.h
template<class K, class V,class Hash = HashFunc<K>>
class unordered_map
{
public:typedef typename li::HashTable<K, pair<const K, V>, MapOfKey, Hash>::Iterator iterator;iterator begin(){return _hash.Begin();}iterator end(){return _hash.End();}
private:li::HashTable<K, pair<const K, V>,MapOfKey,Hash> _hash;
};
//unordered_set.h
template<class K,class Hash = HashFunc<K>>
class unordered_set
{
public:typedef typename li::HashTable<K,const K, SetOfKey, Hash>::Iterator iterator;iterator begin(){return _hash.Begin();}iterator end(){return _hash.End();}
private:li::HashTable<K,const K,SetOfKey,Hash> _hash;
};
li::HashTable<K, pair<const K, V>,MapOfKey,Hash> _hash; //unordered_map
li::HashTable<K,const K,SetOfKey,Hash> _hash; // unordered_set
步驟4:實現const迭代器
增加迭代器的模板參數
template<class K, class T, class KeyOfT, class Hash>
class HashTable; //對哈希表聲明template<class K, class T,class Ref,class Ptr, class KeyOfT, class Hash>
struct HashTableIterator
{typedef HashNode<T> Node;typedef HashTable<K, T, KeyOfT, Hash> ht;typedef HashTableIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;Node* _node; //結點的指針const ht* _ht; //哈希表指針HashTableIterator(Node* node,const ht* ht):_node(node),_ht(ht){ }Ref operator*(){return _node->_data;}Ptr operator->(){return &_node->_data;}
};
步驟5:實現const Begin()和const End()
template<class K,class T,class KeyOfT,class Hash>
class HashTable
{typedef HashNode<T> Node;template<class K, class T, class KeyOfT, class Hash>friend struct HashTableIterator; //友元聲明,方便迭代器訪問哈希表中的私有成員public:typedef HashTableIterator<K, T, T&, T*, KeyOfT, Hash> Iterator;typedef HashTableIterator<K, T, const T&, const T*, KeyOfT, Hash> ConstIterator;ConstIterator Begin() const{for (int i = 0; i < _tables.size(); i++){Node* cur = _tables[i];if (cur){return ConstIterator(cur, this);}}return End();}ConstIterator End() const{return ConstIterator(nullptr, this);}
};
步驟6:封裝哈希const_iterator實現unordered_map和unordered_set的const_iterator
//unordered_map.h
template<class K, class V,class Hash = HashFunc<K>>
class unordered_map
{
public:typedef typename li::HashTable<K, pair<const K, V>, MapOfKey, Hash>::ConstIterator const_iterator;const_iterator begin() const{return _hash.Begin();}const_iterator end() const{return _hash.End();}
private:li::HashTable<K, pair<const K, V>,MapOfKey,Hash> _hash;
};
//unordered_set.h
template<class K,class Hash = HashFunc<K>>
class unordered_set
{
public:typedef typename li::HashTable<K,const K, SetOfKey, Hash>::ConstIterator const_iterator;const_iterator begin() const{return _hash.Begin();}const_iterator end() const{return _hash.End();}
private:li::HashTable<K,const K,SetOfKey,Hash> _hash;
};
2.3 unordered_map支持[]
unordered_map要支持[]主要需要修改insert返回值支持,修改HashTable中insert的返回值為pair<Iterator,bool> insert(const T& data),有了insert支持unordered_map的[]就很好實現了
代碼如下:
pair<Iterator,bool> insert(const T& data)
{Hash hs;KeyOfT kot;Iterator it = find(kot(data)); //find函數的返回值也要進行修改,修改成Iteratorif (it != End())return { it,false };//負載因子為1就擴容if (_n == _tables.size()){unsigned long newsize = _stl_next_prime(_tables.size() + 1);vector<Node*> newtables(newsize, nullptr);for (int i = 0; i < _tables.size(); i++){//遍歷舊表,將數據挪到新表Node* cur = _tables[i];while (cur){Node* next = cur->_next;size_t hashi = hs(kot(cur->_data)) % newtables.size();cur->_next = newtables[hashi];newtables[hashi] = cur;cur = next;}_tables[i] = nullptr;}_tables.swap(newtables);}
//unordered_map.h
V& operator[](const K& key)
{pair<iterator, bool> ret = _hash.insert(make_pair(key, V()));return ret.first->second;
}
既然哈希表中的inser的返回值修改了,那么對應的unordered_map和unordered_set的insert函數的返回值也要進行修改
(三)結束語
用哈希表來模擬實現這兩個容器,就得先學習底層的哈希表,不了解哈希表的友友可以看我的上一篇文章https://blog.csdn.net/muzi_liii/article/details/147519118?spm=1001.2014.3001.5501。該模擬實現簡易的unordered_map和unordered_set就完成了。一起學習吧!