本文最后更新于 2024-08-15T14:16:45+08:00
哈希(hash)又名散列,所以哈希表(hash表)又名散列表。hash表是以键值对存储的数据结构。哈希表与数组的区别就在于哈希表的关键字可以是任何数据类型。
STL 在STL中,C++已经准备好了一个用红黑树维护的关联式容器,称为map。
操作
操作
功能
map<data1,data2> hash
定义一个关键字的类型为data1,值的类型为data2的hash表hash
hash[key]=value或hash.insert(make_pair(key,value))
在s中插入一个关键字为x,值为y的键值对
hash.erase(key)
删除关键字为key的所有元素
hash.erase(it)
删除迭代器为it的元素
hash.erase(first,last)
删除迭代器在[first,last)范围内的所有元素
hash.clear()
清空hash表
hash.count(key)
返回键为key的键值对的数量,若无则返回-1
hash.begin()或hash.cbegin()
指向hash首元素的迭代器
hash.end()或hash.end()
指向hash尾端占位符的迭代器,没有元素
hash.rbegin()或hash.crbegin()
指向hash末元素的迭代器
hash.rend()或hash.crend()
hash首元素的前一个,没有元素
hash.find(key)
返回关键字为key的元素的迭代器,若无则返回hash.end()
hash.lower_bound(key)
返回首个关键字不小于key的元素的迭代器,若无则返回hash.end()
hash.upper_bound(key)
返回首个关键字大于key的元素的迭代器,若无则返回hash.end()
hash.size()
hash的元素个数
map<data1,data2>::iterator it
定义一个关键字的类型为data1,值的类型为data2的hash表的迭代器it
遍历方式 方法一:用迭代器遍历
参考代码:
1 2 3 4 map<data1, data2> hash; map<data1, data2>::iterator it;for (i = hash.begin (); i < hash.end (); i++) ;
方法一:使用范围for循环
参考代码:
1 2 3 map<data1, data2> hash;for (auto x : hash) ;
手写哈希 我们为了使键值对对应到内存中,我们就需要利用键为键值对计算索引,这个计算方法就称之为哈希(hash)函数或散列函数。
当然在实际操作中,往往会出现两个键不同而索引相同的情况,这种情况被称之为哈希(hash)冲突。
hash函数的构造方法 方法一:直接以键为地址 参考代码:
1 2 3 4 void insert (int key, int val) { hash[key] = val; }
方法二:将键经过线性运算后作为地址 参考代码:
1 2 3 4 5 const int a, b;void insert (int key, int val) { hash[a * key + b] = val; }
方法三:离散化 以每个键键值对的值的排名作为新的值,可见离散化本质上与方法一相同,只处理了值过大或不符合计算需求的问题。
在不关注输入顺序时,我们可以创建原数组的副本,排序去重再将排名存回原数组。
参考代码:
1 2 3 4 5 6 int n, a[10000005 ], tmp[10000005 ];memccpy (tmp, a, sizeof (tmp));sort (tmp + 1 , tmp + n + 1 );int m = unique (tmp + 1 , tmp + n + 1 ) - (tmp + 1 );for (int i = 1 ; i <= n; i++) a[i] = lower_bound (tmp + 1 , tmp + m + 1 , a[i]) - tmp;
在关注输入顺序时,我们可以创建原数组的副本同时记录每个元素的位置,排序后再将位置存回原数组。
参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 struct data { int id, val; } tmp[10000005 ];int n, a[10000005 ];bool cmp (data x, data y) { return (x.val < y.val) || (x.val == y.val && x.id < y.id); }int main (int argc, char const *argv[]) { for (int i = 1 ; i <= n; i++) tmp[i].id = i, tmp[i].val = a[i]; sort (tmp + 1 , tmp + n + 1 , cmp); for (int i = 1 ; i <= n; i++) a[tmp[i].id] = i; return 0 ; }
方法四:数字分析法 取键的中间几位作为地址。
参考代码:
1 2 3 4 5 6 7 8 9 10 11 const int x, y;int ten1, ten2;void insert (int key, int val) { hash[key % ten2 / ten1] = val; }int main (int argc, char const *argv[]) { ten1 = pow (10 , x), ten2 = pow (10 , y + 1 ); return 0 ; }
方法五:平方取中法 先将键平方再使用方法四。
参考代码:
1 2 3 4 5 6 7 8 9 10 11 const int x, y;int ten1, ten2;void insert (int key, int val) { hash[key * key % ten2 / ten1] = val; }int main (int argc, char const *argv[]) { ten1 = pow (10 , x), ten2 = pow (10 , y + 1 ); return 0 ; }
方法六:取模法 将键对于同一模数取模作为地址。
参考代码:
1 2 3 4 5 6 7 8 9 10 11 const int x, y;int ten1, ten2;void insert (int key, int val) { hash[key * key % ten2 / ten1] = val; }int main (int argc, char const *argv[]) { ten1 = pow (10 , x), ten2 = pow (10 , y + 1 ); return 0 ; }
方法七:字符串哈希 适用于所有以数组为键的键值对。
将键中的每一个元素作为一位转化成b进制数再使用方法六,要注意的是要满足(b,p)=1(p为模数)。
参考代码:
1 2 3 4 5 const int mod;void insert (int key, int val) { hash[key % mod] = val; }
方法八:折叠法 以定长的位数对键进行拆分并相加作为地址,例如123456789以4为定长折叠后变为1+2345+6789=9135.
参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 const int len;int ten;void insert (int k, int val) { int key = 0 ; while (k) key += k % ten, k /= ten; hash[k] = val; }int main (int argc, char const *argv[]) { ten = pow (10 , len + 1 ); return 0 ; }
方法九:随机法 以随机数作为地址,或者以随机数作为值。
参考代码1:
1 2 3 4 5 void insert (int val) { srand (time (0 )); hash[rand ()] = val; }
参考代码2:
1 2 3 4 5 void insert (int key) { srand (time (0 )); hash[key] = rand (); }
处理hash冲突的方法 方法一:开放地址法 再开放一个地址给冲突的元素,其中有线性探测,二次探测,随机探测三种方法。
线性探测就是寻找冲突元素下一个地址存储。
参考代码:
1 2 3 4 5 6 7 void insert (int key, int val) { while (hash[key]) key++; hash[key] = val; }
二次探测就是在线性探测的基础上加一个寻找上一个地址的操作。
线性探测就是寻找冲突元素下一个地址存储。
参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 const int MAXN;int hash[MAXN];void insert (int key, int val) { while (hash[key]) key++; if (key < MAXN) { hash[key] = val; return ; } while (hash[--key]) ; hash[key] = val; }
随机探测就是随机寻找一个空地址存储。
参考代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 const int MAXN;int hash[MAXN];void insert (int key, int val) { if (hash[key] == 0 ) { hash[key] = val; return ; } srand (time (0 )); key = rand () % MAXN; while (hash[key]) key = rand () % MAXN; hash[key] = val; }
方法二:再哈希法 遇到冲突换一个hash函数计算地址。
参考代码:
1 2 3 4 5 6 7 8 9 int h1 (int key) ;int h2 (int key) ; void insert (int key, int val) { int k = h1 (key); if (hash[k]) k = h2 (key); hash[k] = val; }
方法三:邻接表法 将每一个键对应一个链表,再将键相同的元素存储到链表里。
参考代码:
1 2 3 4 5 6 vector<data> hash[10000005 ];void insert (int key, data val) { hash[key].push_back (val); }
方法四:公共区域法 建立一个公共区域专门存储冲突元素。
参考代码:
1 2 3 4 5 6 7 const int mod;void insert (int key, data val) { if (hash[key]) hash[key + mod] = val; }