目录
一. 哈希的概念
- 插入元素
- 根据待插入元素的关键码,以此函数计算出该元素的存储位置并按此位置进行存放
- 搜索元素
- 对元素的关键码进行同样的计算,把求得的函数值当做元素的存储位置,在结构中按此位置取元素比较,若关键码相等,则搜索成功

这种除留余数法虽然可以解决空间浪费的问题,但是却有不同的值映射到相同位置的风险~
二. 哈希冲突
当我们再插入数据14映射时会把原本位置上的4覆盖掉,造成哈希冲突~
三. 哈希冲突解决方法
闭散列
线性探测:从发生冲突的位置开始,依次向后探测,直到寻找到下一个空位置为止。
插入是解决了,那么删除的问题呢?
下面的状态标记帮我们解决了问题~
//设置状态 enum State { EMPTY, EXIST, DELETE }; template<class K,class V> struct HashDate { pair<K, V> _data; State _state = EMPTY; }; template<class K, class V> class HashTable { public: private: vector<HashDate<K,V>> _table; };当遇到删除的标记,查找则继续往下遍历~
插入
HashTable(size_t size = 10) { _table.resize(size); } bool insert(const pair<K, V>& kv) { //控制在载荷因子允许范围内 if ((n * 10 / _table.size()) >= 7) { //开始扩容 size_t newsize = _table.size() * 2; } //要映射的下标位置 size_t hashi = kv.first % _table.size(); //判断当前映射的下标位置的状态 while(_table[hashi]->_state == EXIST) { hashi++; //到尾部时能够返回到头部 hashi%= _table.size(); } //当前映射下标可以插入 _table[hashi]._data = kv; _table[hashi]._state = EXIST; n++; }ps:这里取模我们只能用size而不能用capacity
如果用capacity取模会插入在size之外的下标,而vector的[ ]特性中对会>size的越界访问进行报错,所以我们只好把size近似当作capacity来处理了(初始化别给0即可)~
下面我们再从插入的基础上思考扩容问题~
扩容
bool insert(const pair<K, V>& kv) { //如果已存在,无法插入 if (Find(kv.first)) { return false; } //控制在载荷因子允许范围内 if ((n * 10 / _table.size()) >= 7) { //开始扩容 size_t newsize = _table.size() * 2; vector<HashDate> _newtable(newsize); //遍历旧表,插入新表 //.......... _table.swap(_newtable); } //要映射的下标位置 size_t hashi = kv.first % _table.size(); //判断当前映射的下标位置的状态 while(_table[hashi]->_state == EXIST) { hashi++; //到尾部时能够返回到头部 hashi%= _table.size(); } //当前映射下标可以插入 _table[hashi]._data = kv; _table[hashi]._state = EXIST; n++; }这种扩容方式的麻烦之处就在于遍历旧表插入新表时还得按下面插入的内容再走一次~
bool insert(const pair<K, V>& kv) { //如果已存在,无法插入 if (Find(kv.first)) { return false; } //控制在载荷因子允许范围内 if ((n * 10 / _table.size()) >= 7) { //开始扩容 HashTable<K, V> newHT(_table.size() * 2); //遍历旧表,插入新表 for (auto& e : _table) { //复用 newHT.insert(e._data); } _table.swap(newHT._table); } //要映射的下标位置 size_t hashi = kv.first % _table.size(); //判断当前映射的下标位置的状态 while(_table[hashi]._state == EXIST) { hashi++; //到尾部时能够返回到头部 hashi%= _table.size(); } //当前映射下标可以插入 _table[hashi]._data = kv; _table[hashi]._state = EXIST; n++; }这里我们重新生成一个对象,然后设置该对象内_table的size为旧表的两倍,这样遍历旧表插入新表的时候就可以用成员函数insert进行复用,最后再交换数据即可~
第一种方式只是在一个对象内生成另一个vector,然后对象内部进行交换。而第二种是有两个对象,跨对象的数据进行交换~区别在于后者可以使用其成员函数,减少代码冗余~
寻找
HashDate<K, V>* Find(const K& key) { //要映射的下标位置 size_t hashi = key % _table.size(); //判断当前映射的下标位置的状态 while (_table[hashi]._state != EMPTY) { if (_table[hashi]._data.first == key && _table[hashi]._state == EXIT) { return &_table[hashi]; } hashi++; //到尾部时能够返回到头部 hashi %= _table.size(); } return nullptr; }由于我们还没有模拟实现迭代器,这里我们利用Find函数来帮助我们测试插入效果~
删除
bool erase(const K& key)
{
//利用find快速查找
HashDate<K, V>* ret = Find(key);
if (ret)
{
ret->_state = DELETE;
n--;
return true;
}
else
{
return false;
}
}

扩展
通常我们是利用整型key取模来映射下标位置,那如果我们的key值变成string或自定义类(日期类)的时候我们是无法把它们强制转化为整型去取模的,这时候应该如何去做呢?
我们可以给哈希表再加上一层映射,通过映射让可以强转为整型的key值转化为可以被取模的整型,让无法被强转为整型的key值进行特殊处理~
那么关于字符串我们应该采用哪种方式进行最终的取模呢?
一般是让各个字符的ascll码值相加,这样就可以确保每个字符串的唯一性的同时还可以转化为整型进行取模映射下标位置~
//不可强转 struct HashString { size_t operator()(const string& s) { size_t hash = 0; for (auto e : s) { hash += e; //*5是为了避免“abcd","acbd"这种例子的发生 hash *= 5; } return hash; } };
不过对于string这种采用的key值,我们一般用另一种方式进行转化~
// 特化 template<> struct HashFunc<string> { size_t operator()(const string& s) { size_t hash = 0; for (auto e : s) { hash += e; hash *= 5; } return hash; } };
可以利用特化的方式进行更准确的参数匹配~
闭散列代码
#pragma once
namespace close
{
//设置状态
enum State
{
EMPTY,
EXIST,
DELETE
};
template<class K,class V>
struct HashDate
{
pair<K, V> _data;
State _state = EMPTY;
};
template<class K>
//可强转
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
// 特化
template<>
struct HashFunc<string>
{
size_t operator()(const string& s)
{
size_t hash = 0;
for (auto e : s)
{
hash += e;
hash *= 5;
}
return hash;
}
};
//
////不可强转
//struct HashString
//{
// size_t operator()(const string& s)
// {
// size_t hash = 0;
// for (auto e : s)
// {
// hash += e;
// //*5是为了避免“abcd","acbd"这种例子的发生
// hash *= 5;
// }
// return hash;
// }
//};
template<class K, class V,class Hash = HashFunc<K>>
class HashTable
{
public:
HashTable(size_t size = 10)
{
_table.resize(size);
}
bool erase(const K& key)
{
//利用find快速查找
HashDate<K, V>* ret = Find(key);
if (ret)
{
ret->_state = DELETE;
n--;
return true;
}
else
{
return false;
}
}
HashDate<K, V>* Find(const K& key)
{
Hash hs;
// 线性探测
size_t hashi = hs(key) % _table.size();
//判断当前映射的下标位置的状态
while (_table[hashi]._state != EMPTY)
{
if (_table[hashi]._data.first == key && _table[hashi]._state == EXIST)
{
return &_table[hashi];
}
hashi++;
//到尾部时能够返回到头部
hashi %= _table.size();
}
return nullptr;
}
bool insert(const pair<K, V>& kv)
{
//如果已存在,无法插入
if (Find(kv.first))
{
return false;
}
//控制在载荷因子允许范围内
if ((n * 10 / _table.size()) >= 7)
{
////开始扩容
//size_t newsize = _table.size() * 2;
//vector<HashDate> _newtable(newsize);
////遍历旧表,插入新表
////..........
//_table.swap(_newtable);
//
//开始扩容
HashTable<K, V,Hash> newHT(_table.size() * 2);
//遍历旧表,插入新表
for (auto& e : _table)
{
//复用
newHT.insert(e._data);
}
_table.swap(newHT._table);
}
Hash hs;
//要映射的下标位置
size_t hashi = hs(kv.first) % _table.size();
//判断当前映射的下标位置的状态
while(_table[hashi]._state == EXIST)
{
hashi++;
//到尾部时能够返回到头部
hashi%= _table.size();
}
//当前映射下标可以插入
_table[hashi]._data = kv;
_table[hashi]._state = EXIST;
n++;
}
private:
vector<HashDate<K,V>> _table;
//有效个数
size_t n;
};
void test1()
{
int a[] = { 1,4,24,34,7,44,17,37 };
HashTable<int, int> ht;
for (auto e : a)
{
ht.insert(make_pair(e, e));
}
ht.erase(1);
ht.erase(34);
for (auto e : a)
{
auto ret = ht.Find(e);
if (ret)
{
cout << ret->_data.first << ":E" << endl;
}
else
{
cout << e<< ":D" << endl;
}
}
cout << endl;
}
void test2()
{
HashTable<string, string> dict;
dict.insert(make_pair("sort", "排序"));
dict.insert(make_pair("string", "字符串"));
}
}
开散列

namespace open
{
template<class K, class V>
struct HashNode
{
pair<K, V> _kv;
HashNode<K, V>* _next;
HashNode(const pair<K, V>& kv)
:_next(nullptr)
,_kv(kv)
{}
};
template<class K,class V>
class HashTable
{
public:
private:
//直接套用list
//vector<list<pair<K, V>>> _table;
//自己写一个,指针数组
vector<HashNode<K,V>*> _table;
size_t n;
};
}
这里我们不直接用容器list的原因是方便自己写一个迭代器
插入

bool insert(const pair<K,V>& kv)
{
size_t hashi = kv.first % _table.size();
Node* newnode = new Node(kv);
//头插
newnode->_next = _table[hashi];
_table[hashi] = newnode;
n++;
return true;
}
寻找
//寻找
//我们需要查到的是值,而这些值在节点中
Node* Find(const K& key)
{
//映射下标
size_t hashi = key % _table.size();
Node* cur = _table[hashi];
while (cur)
{
if (cur->_kv.first == key)
{
return cur;
}
cur = cur->_next;
}
return nullptr;
}
扩容
//插入 bool insert(const pair<K,V>& kv) { if (Find(kv.first)) { return false; } //负载因子为1 //扩容 if (n == _table.size()) { vector<Node*> _newtable(_table.size()*2,nullptr); for (size_t i = 0; i < _table.size(); i++) { //遍历每一个桶 Node* cur = _table[i]; //把桶里的节点都移动到新表中 while (cur) { //保存好下一节点 Node* next = cur->_next; //映射当前节点在新表中的位置 size_t hashi = cur->_kv.first % _newtable.size(); //头插 cur->_next = _newtable[hashi]; _newtable[hashi] = cur; //对该桶下一节点进行转移 cur = next; } //节点全部转移完毕,清空旧表指针指向 _table[i] = nullptr; } //两表交换完成扩容 _table.swap(_newtable); } size_t hashi = kv.first % _table.size(); Node* newnode = new Node(kv); //头插 newnode->_next = _table[hashi]; _table[hashi] = newnode; n++; return true; }这一次的扩容与线性探测不太一样,之前我们为了方便插入到新表选择重新构成一个类对象来复用Insert,而这一次是为了避免节点的浪费转而采用移动节点的方式进行插入新表~
删除
//删除
bool erase(const K& key)
{
//找到要删除的映射位置(桶)
size_t hashi = key % _table.size();
//遍历当前桶节点
Node* cur = _table[hashi];
//保留前一个节点,使其删除后链接
Node* prev = nullptr;
while (cur)
{
if (cur->_kv.first == key)
{
//若非头删
if (prev)
{
prev->_next = cur->_next;
}
//若恰好头删
else
{
_table[hashi] = cur->_next;
}
//节点删除
delete cur;
n--;
return true;
}
else
{
prev = cur;
cur = cur->_next;
}
}
return false;
}
删除我们得考虑在这之后节点之间的链接~

扩展
最后我们再来写一下关于字符串取模的类模板~
//可强转
template<class K>
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
// 特化
template<>
struct HashFunc<string>
{
size_t operator()(const string& s)
{
size_t hash = 0;
for (auto e : s)
{
hash += e;
hash *= 5;
}
return hash;
}
};
template<class K,class V,class Hash = HashFunc<K>>
class HashTable...


开散列代码
namespace open
{
template<class K, class V>
struct HashNode
{
pair<K, V> _kv;
HashNode<K, V>* _next;
HashNode(const pair<K, V>& kv)
:_next(nullptr)
, _kv(kv)
{}
};
//可强转
template<class K>
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
// 特化
template<>
struct HashFunc<string>
{
size_t operator()(const string& s)
{
size_t hash = 0;
for (auto e : s)
{
hash += e;
hash *= 5;
}
return hash;
}
};
template<class K,class V,class Hash = HashFunc<K>>
class HashTable
{
typedef HashNode<K, V> Node;
public:
HashTable()
{
_table.resize(10, nullptr);
n = 0;
}
~HashTable()
{
for (size_t i = 0; i < _table.size(); i++)
{
Node* cur = _table[i];
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_table[i] = nullptr;
}
}
//删除
bool erase(const K& key)
{
Hash hh;
//找到要删除的映射位置(桶)
size_t hashi = hh(key) % _table.size();
//遍历当前桶节点
Node* cur = _table[hashi];
//保留前一个节点,使其删除后链接
Node* prev = nullptr;
while (cur)
{
if (cur->_kv.first == key)
{
//若非头删
if (prev)
{
prev->_next = cur->_next;
}
//若恰好头删
else
{
_table[hashi] = cur->_next;
}
//节点删除
delete cur;
n--;
return true;
}
else
{
prev = cur;
cur = cur->_next;
}
}
return false;
}
//寻找
//我们需要查到的是值,而这些值在节点中
Node* Find(const K& key)
{
Hash hh;
//映射下标
size_t hashi = hh(key) % _table.size();
Node* cur = _table[hashi];
while (cur)
{
if (cur->_kv.first == key)
{
return cur;
}
cur = cur->_next;
}
return nullptr;
}
//插入
bool insert(const pair<K,V>& kv)
{
Hash hh;
if (Find(kv.first))
{
return false;
}
//负载因子为1
//扩容
if (n == _table.size())
{
vector<Node*> _newtable(_table.size()*2,nullptr);
for (size_t i = 0; i < _table.size(); i++)
{
//遍历每一个桶
Node* cur = _table[i];
//把桶里的节点都移动到新表中
while (cur)
{
//保存好下一节点
Node* next = cur->_next;
//映射当前节点在新表中的位置
size_t hashi = hh(cur->_kv.first) % _newtable.size();
//头插
cur->_next = _newtable[hashi];
_newtable[hashi] = cur;
//对该桶下一节点进行转移
cur = next;
}
//节点全部转移完毕,清空旧表指针指向
_table[i] = nullptr;
}
//两表交换完成扩容
_table.swap(_newtable);
}
size_t hashi = hh(kv.first) % _table.size();
Node* newnode = new Node(kv);
//头插
newnode->_next = _table[hashi];
_table[hashi] = newnode;
n++;
return true;
}
private:
//直接套用list
//vector<list<pair<K, V>>> _table;
//自己写一个,指针数组
vector<Node*> _table;
size_t n;
};
void Test1()
{
//HashTable<int, int> ht;
//int a[] = { 1,4,24,34,7,44,17,37 };
//for (auto e : a)
//{
// ht.insert(make_pair(e, e));
//}
///*ht.insert(make_pair(5, 5));
//ht.insert(make_pair(15, 15));
//ht.insert(make_pair(25, 25));*/
//ht.erase(1);
//ht.erase(4);
//ht.erase(24);
//ht.erase(34);
HashTable<string, string> dict;
dict.insert(make_pair("sort", "排序"));
dict.insert(make_pair("string", "字符串"));
}
}
四. 封装unordered_map/_set
我们已经把unordered_map/_set底层的哈希层构建好了,接下来就是根据二者其特点用哈希进行封装~
底层哈希的复用
#pragma once
#include "HashTable.h"
namespace lj
{
template<class K, class Hash = open::HashFunc<K>>
class unordered_set
{
struct setkey
{
const K& operator()(const K& key)
{
return key;
}
};
private:
//调用底层哈希
open::HashTable<K, K, setkey,Hash> _ht;
};
}
#pragma once
#include "HashTable.h"
namespace lj
{
template<class K, class V, class Hash = open::HashFunc<K>>
class unordered_map
{
struct mapkey
{
const K& operator()(const pair<K,V>& kv)
{
return kv.first;
}
};
private:
//调用底层哈希
open::HashTable<K, pair<K,V>, mapkey,Hash> _ht;
};
}

迭代器
//迭代器
template <class K,class T,class whokey,class Hash>
struct _HTIterator
{
typedef HashNode<T> Node;
typedef HashTable<K, T, whokey, Hash> HT;
typedef _HTIterator<K, T, whokey, Hash> Self;
Node* _node;
HT* _ht;
_HTIterator(Node*_node,HT*_ht)
:_node
,ht(_ht)
{}
T& operator*()
{
return _node->_data;
}
Self& operator++()
{
whokey wk;
Hash hh;
//若当前桶还没走完
if (_node->_next)
{
//继续走当前桶内节点
_node = _node->_next;
}
//若当前桶已走完,则前往下一个桶
else
{
//映射当前位置
size_t hashi = hh(wk(_node->_data)) % _ht->_table.size();
hashi++;
while (hashi < _ht->_table.size())
{
//若当前桶内有节点
if (_ht->_table[hashi])
{
_node = _ht->_table[hashi];
//找到下一个桶的位置
break;
}
else
{
hashi++;
}
}
//如果到最后都找不到下一个桶
if (hashi == _ht->_table.size())
{
//那么下一个即为空
return nullptr;
}
}
return *this;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
};
begin/end
iterator begin()
{
//找到第一个桶的第一个节点
for (size_t i = 0; i < _table.size(); i++)
{
if (_table[i])
{
return iterator(_table[i], this);
}
}
return end();
}
iterator end()
{
return iterator(nullptr, this);
}


运算符[ ]
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()));
return ret.first->second;
}
pair<iterator,bool> insert(const T& data)
{
whokey wk;
Hash hh;
iterator it = Find(wk(data));
if (it != end())
{
return make_pair(it, false);
}
/*if (Find(wk(data))!=end())
{
return false;
}*/
//负载因子为1
//扩容
if (n == _table.size())
{
vector<Node*> _newtable(_table.size()*2,nullptr);
for (size_t i = 0; i < _table.size(); i++)
{
//遍历每一个桶
Node* cur = _table[i];
//把桶里的节点都移动到新表中
while (cur)
{
//保存好下一节点
Node* next = cur->_next;
//映射当前节点在新表中的位置
size_t hashi = hh(wk(cur->_data)) % _newtable.size();
//头插
cur->_next = _newtable[hashi];
_newtable[hashi] = cur;
//对该桶下一节点进行转移
cur = next;
}
//节点全部转移完毕,清空旧表指针指向
_table[i] = nullptr;
}
//两表交换完成扩容
_table.swap(_newtable);
}
size_t hashi = hh(wk(data)) % _table.size();
Node* newnode = new Node(data);
//头插
newnode->_next = _table[hashi];
_table[hashi] = newnode;
n++;
return make_pair(iterator(newnode, this), true);
//return true;
}
五. 全部代码
"MYunordered_map.h"
#pragma once
#include "HashTable.h"
namespace lj
{
template<class K, class V, class Hash = open::HashFunc<K>>
class unordered_map
{
struct mapkey
{
const K& operator()(const pair<K,V>& kv)
{
return kv.first;
}
};
public:
typedef typename open::HashTable<K, pair<const K, V>, mapkey, Hash>::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);
}
iterator find(const K& key)
{
return _ht.Find(key);
}
bool erase(const K& key)
{
return _ht.erase(key);
}
V& operator[](const K& key)
{
pair<iterator, bool> ret = insert(make_pair(key, V()));
return ret.first->second;
}
private:
//调用底层哈希
open::HashTable<K, pair<const K,V>, mapkey,Hash> _ht;
};
void test_map1()
{
unordered_map<string, string> dict;
dict.insert(make_pair("apple", "苹果"));
dict.insert(make_pair("left", "左边"));
dict.insert(make_pair("right", "右边"));
for (auto& kv : dict)
{
cout << kv.first << ":" << kv.second << endl;
}
}
void test_map2()
{
string arr[] = { "ppp","ddd","aaa","aaa" ,"aaa" ,"aaa" ,"ddd" };
unordered_map<string, int> conutmap;
for (auto& e : arr)
{
conutmap[e]++;
}
for (auto& kv : conutmap)
{
cout << kv.first << ":" << kv.second << endl;
}
}
}
"MYunoredered_set.h"
#pragma once
#include "HashTable.h"
namespace lj
{
template<class K, class Hash = open::HashFunc<K>>
class unordered_set
{
struct setkey
{
const K& operator()(const K& key)
{
return key;
}
};
public:
typedef typename open::HashTable<K, const K, setkey, Hash>::iterator iterator;
iterator begin()
{
return _ht.begin();
}
iterator end()
{
return _ht.end();
}
pair<iterator, bool> Insert(const K& key)
{
return _ht.insert(key);
}
iterator find(const K& key)
{
return _ht.Find(key);
}
bool erase(const K& key)
{
return _ht.erase(key);
}
private:
//调用底层哈希
open::HashTable<K, const K, setkey,Hash> _ht;
};
void test_set1()
{
unordered_set<int> us;
us.Insert(3);
us.Insert(1);
us.Insert(5);
us.Insert(15);
us.Insert(45);
us.Insert(7);
unordered_set<int>::iterator it = us.begin();
while (it != us.end())
{
//*it += 100;
cout << *it << " ";
++it;
}
cout << endl;
if (us.find(1) != us.end())
{
cout << "找到了" << endl;
}
for (auto e : us)
{
cout << e << " ";
}
cout << endl;
}
}
"HashTable.h"
#pragma once
namespace close
{
//设置状态
enum State
{
EMPTY,
EXIST,
DELETE
};
template<class K,class V>
struct HashDate
{
pair<K, V> _data;
State _state = EMPTY;
};
template<class K>
//可强转
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
// 特化
template<>
struct HashFunc<string>
{
size_t operator()(const string& s)
{
size_t hash = 0;
for (auto e : s)
{
hash += e;
hash *= 5;
}
return hash;
}
};
//
////不可强转
//struct HashString
//{
// size_t operator()(const string& s)
// {
// size_t hash = 0;
// for (auto e : s)
// {
// hash += e;
// //*5是为了避免“abcd","acbd"这种例子的发生
// hash *= 5;
// }
// return hash;
// }
//};
template<class K, class V,class Hash = HashFunc<K>>
class HashTable
{
public:
HashTable(size_t size = 10)
{
_table.resize(size);
}
bool erase(const K& key)
{
//利用find快速查找
HashDate<K, V>* ret = Find(key);
if (ret)
{
ret->_state = DELETE;
n--;
return true;
}
else
{
return false;
}
}
HashDate<K, V>* Find(const K& key)
{
Hash hs;
// 线性探测
size_t hashi = hs(key) % _table.size();
//判断当前映射的下标位置的状态
while (_table[hashi]._state != EMPTY)
{
if (_table[hashi]._data.first == key && _table[hashi]._state == EXIST)
{
return &_table[hashi];
}
hashi++;
//到尾部时能够返回到头部
hashi %= _table.size();
}
return nullptr;
}
bool insert(const pair<K, V>& kv)
{
//如果已存在,无法插入
if (Find(kv.first))
{
return false;
}
//控制在载荷因子允许范围内
if ((n * 10 / _table.size()) >= 7)
{
////开始扩容
//size_t newsize = _table.size() * 2;
//vector<HashDate> _newtable(newsize);
////遍历旧表,插入新表
////..........
//_table.swap(_newtable);
//
//开始扩容
HashTable<K, V,Hash> newHT(_table.size() * 2);
//遍历旧表,插入新表
for (auto& e : _table)
{
//复用
newHT.insert(e._data);
}
_table.swap(newHT._table);
}
Hash hs;
//要映射的下标位置
size_t hashi = hs(kv.first) % _table.size();
//判断当前映射的下标位置的状态
while(_table[hashi]._state == EXIST)
{
hashi++;
//到尾部时能够返回到头部
hashi%= _table.size();
}
//当前映射下标可以插入
_table[hashi]._data = kv;
_table[hashi]._state = EXIST;
n++;
}
private:
vector<HashDate<K,V>> _table;
//有效个数
size_t n;
};
void test1()
{
int a[] = { 1,4,24,34,7,44,17,37 };
HashTable<int, int> ht;
for (auto e : a)
{
ht.insert(make_pair(e, e));
}
ht.erase(1);
ht.erase(34);
for (auto e : a)
{
auto ret = ht.Find(e);
if (ret)
{
cout << ret->_data.first << ":E" << endl;
}
else
{
cout << e<< ":D" << endl;
}
}
cout << endl;
}
void test2()
{
HashTable<string, string> dict;
dict.insert(make_pair("sort", "排序"));
dict.insert(make_pair("string", "字符串"));
}
}
namespace open
{
//可强转
template<class K>
struct HashFunc
{
size_t operator()(const K& key)
{
return (size_t)key;
}
};
// 特化
template<>
struct HashFunc<string>
{
size_t operator()(const string& s)
{
size_t hash = 0;
for (auto e : s)
{
hash += e;
hash *= 5;
}
return hash;
}
};
// T -> K
// T -> pair<K, V>
template<class T>
struct HashNode
{
HashNode<T>* _next;
T _data;
HashNode(const T& data)
:_next(nullptr)
, _data(data)
{}
};
// 前置声明
template<class K, class T, class whokey, class Hash>
class HashTable;
//迭代器
template <class K,class T,class whokey,class Hash>
struct _HTIterator
{
typedef HashNode<T> Node;
typedef HashTable<K, T, whokey, Hash> HT;
typedef _HTIterator<K, T, whokey, Hash> Self;
Node* _node;//指向节点
HT* _ht;//指向桶
_HTIterator(Node* node, 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
{
whokey wk;
Hash hh;
//映射当前位置
size_t hashi = hh(wk(_node->_data)) % _ht->_table.size();
hashi++;
while (hashi < _ht->_table.size())
{
//若当前桶内有节点
if (_ht->_table[hashi])
{
_node = _ht->_table[hashi];
//找到下一个桶的位置
break;
}
else
{
hashi++;
}
}
//如果到最后都找不到下一个桶
if (hashi == _ht->_table.size())
{
//那么下一个即为空
_node = nullptr;
}
}
return *this;
}
bool operator!=(const Self& s)
{
return _node != s._node;
}
};
template<class K,class T,class whokey,class Hash>
class HashTable
{
//前置声明
template<class K, class T, class whokey, class Hash>
friend struct _HTIterator;//友元类
typedef HashNode<T> Node;
public:
typedef _HTIterator<K, T, whokey, Hash> iterator;
iterator begin()
{
//找到第一个桶的第一个节点
for (size_t i = 0; i < _table.size(); i++)
{
if (_table[i])
{
return iterator(_table[i], this);
}
}
return end();
}
iterator end()
{
return iterator(nullptr, this);
}
HashTable()
{
_table.resize(10, nullptr);
n = 0;
}
~HashTable()
{
for (size_t i = 0; i < _table.size(); i++)
{
Node* cur = _table[i];
while (cur)
{
Node* next = cur->_next;
delete cur;
cur = next;
}
_table[i] = nullptr;
}
}
//删除
bool erase(const K& key)
{
whokey wk;
Hash hh;
//找到要删除的映射位置(桶)
size_t hashi = hh(key) % _table.size();
//遍历当前桶节点
Node* cur = _table[hashi];
//保留前一个节点,使其删除后链接
Node* prev = nullptr;
while (cur)
{
if (wk(cur->_data) == key)
{
//若非头删
if (prev)
{
prev->_next = cur->_next;
}
//若恰好头删
else
{
_table[hashi] = cur->_next;
}
//节点删除
delete cur;
n--;
return true;
}
else
{
prev = cur;
cur = cur->_next;
}
}
return false;
}
//寻找
//我们需要查到的是值,而这些值在节点中
iterator Find(const K& key)
{
whokey wk;
Hash hh;
//映射下标
size_t hashi = hh(key) % _table.size();
Node* cur = _table[hashi];
while (cur)
{
if (wk(cur->_data) == key)
{
return iterator(cur,this);
}
cur = cur->_next;
}
return iterator(nullptr,this);
}
//插入
pair<iterator,bool> insert(const T& data)
{
whokey wk;
Hash hh;
iterator it = Find(wk(data));
if (it != end())
{
return make_pair(it, false);
}
/*if (Find(wk(data))!=end())
{
return false;
}*/
//负载因子为1
//扩容
if (n == _table.size())
{
vector<Node*> _newtable(_table.size()*2,nullptr);
for (size_t i = 0; i < _table.size(); i++)
{
//遍历每一个桶
Node* cur = _table[i];
//把桶里的节点都移动到新表中
while (cur)
{
//保存好下一节点
Node* next = cur->_next;
//映射当前节点在新表中的位置
size_t hashi = hh(wk(cur->_data)) % _newtable.size();
//头插
cur->_next = _newtable[hashi];
_newtable[hashi] = cur;
//对该桶下一节点进行转移
cur = next;
}
//节点全部转移完毕,清空旧表指针指向
_table[i] = nullptr;
}
//两表交换完成扩容
_table.swap(_newtable);
}
size_t hashi = hh(wk(data)) % _table.size();
Node* newnode = new Node(data);
//头插
newnode->_next = _table[hashi];
_table[hashi] = newnode;
n++;
return make_pair(iterator(newnode, this), true);
//return true;
}
private:
//直接套用list
//vector<list<pair<K, V>>> _table;
//自己写一个,指针数组
vector<Node*> _table;
size_t n;
};
//void Test1()
//{
// //HashTable<int, int> ht;
// //int a[] = { 1,4,24,34,7,44,17,37 };
// //for (auto e : a)
// //{
// // ht.insert(make_pair(e, e));
// //}
// ///*ht.insert(make_pair(5, 5));
// //ht.insert(make_pair(15, 15));
// //ht.insert(make_pair(25, 25));*/
// //ht.erase(1);
// //ht.erase(4);
// //ht.erase(24);
// //ht.erase(34);
// /*HashTable<string, string> dict;
// dict.insert(make_pair("sort", "排序"));
// dict.insert(make_pair("string", "字符串"));*/
//}
}
test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include <vector>
#include <iostream>
#include<unordered_map>
#include<unordered_set>
#include<set>
#include<map>
using namespace std;
#include "HashTable.h"
#include "MYunordered_map.h"
#include "MYunoredered_set.h"
int main()
{
//test1();
//close::test2();
//open::Test1();
//lj::test_map1();
//lj::test_set1();
lj::test_map2();
return 0;
}








可以利用特化的方式进行更准确的参数匹配~

2560

被折叠的 条评论
为什么被折叠?



