Day_8,哈希表的原理及字典简单实现

什么是哈希表(Hash)

构建一个确定的映射,它能把关键字映射到一个唯一的存储位置,此处的映射就叫做哈希函数,通过这种映射得到的表就叫做哈希表。
那么Hash表有什么优势呢,相比于数组有什么优势呢?
举个例子:
假设此时有 a = { 2,3,5,7,4,9,11} 这样一个数组,我们要找出其中的3是在第几个位置此时该怎么做?
我们需要通过遍历整个a数组,找到值等于3的索引。
如果我们使用Hash表进行储存该数据: 此时我们假设映射函数时 y=k%11 (暂时不用去理会这个函数是怎么产生的),我们把a数组的值带入可以得到 y = {2,3,5,7,4,9,0} ,此时可以构造一个表叫做Hash,那么Hash[2] = 2
Hash[3] = 3
Hash[5] = 5
Hash[7] = 7
Hash[4] = 4
Hash[9] = 9
Hash[0] = 11
现在,同样我们要找出值等于3的值,只需要通过y=k%11 把k=3带入,就可以得到 Hash[3] = 3,时间复杂度是O(1) ,这就是Hash表的妙用,可以构造出一个Key_Value的关系,通过Key快速找到Value
如果此时 a = {2,3,5,7,4,9,11,13} 那么2,13这两个值通过上述函数得到的都是2,此时该怎么办?
①更改映射方式,比如y=k%13
②定义一个新规则,当空间被占据(Hash索引已经被使用),找到下一个最近没被使用的Hash空间,将这个值赋值给这个空间
……
总而言之,更改映射方式

实战案例:通过Hash的原理建立一个字典类

#include <iostream>
#include <string>
using namespace std;

struct key_value
{
	int key;//默认权限是public
	int value;
	key_value(){//初始值
		key = 0;
		value = 0;
	}
};

class Batch
{
public:
	Batch(){
		current_batchsize = -1;
	}
	int current_batchsize;

public:
	key_value k_v[50];


};

class Direction
{
public:
	Direction(){
		init();
	}
	int& operator[] (int i); //对[]重载
	int& operator= (int value);//对=重载
    Batch* batch[10];
	int currentKey;
public:
	void init();
	int hash(int);
};
void Direction::init()
{
	for (int i=0; i<10; ++i)
	{
		batch[i]= new Batch();
	}
}

int& Direction::operator[] (int key)
{
    currentKey = key;

	int batch_num = hash(key);

	int current_batchsize = batch[batch_num]->current_batchsize;
	//有问题,若是有相同的key,应该是改变他的value
	batch[batch_num]->k_v[current_batchsize+1].key = currentKey;
	batch[batch_num]->current_batchsize++;

	for (int i=0;i<50;++i)
	{
		if (batch[batch_num]->k_v[i].key == key)
		{
			return batch[batch_num]->k_v[i].value;
		}
	}
}

int& Direction::operator= (int value)
{
    int batch_num = hash(currentKey);

	int current_batchsize = batch[batch_num]->current_batchsize;
	batch[batch_num]->k_v[current_batchsize+1].value = value;
	for (int i=0;i<50;++i)
	{
		if (batch[batch_num]->k_v[i].key == currentKey)
		{
			batch[batch_num]->k_v[i].value = value;
			return currentKey;
		}
	}
}

int Direction::hash(int input)
{
	return input%11;//映射函数
}


int main()
{
    int a[]={2,3,5,7,4,9,11};
	Direction direction;
	for (int i=0;i<sizeof(a)/sizeof(*a);i++)
        direction[a[i]]=a[i];
    cout<<direction[11]<<endl;
	system("pause");
	return 0;
}

上述方法还存在问题,但是基本是按照Hash的思想实现了字典。

如果方法有误,或者其他好方法,欢迎私聊 谢谢! ^ V ^

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值