实现一个简单的哈希表
2020-09-01 本文已影响0人
贰爷
#include <iostream>
#include <vector>
#include <list>
#include <random>
#include <ctime>
using namespace std;
const int hashsize = 12;
//定一个节点的结构体
template <typename T, typename U>
struct HashNode
{
T _key;
U _value;
};
//使用拉链法实现哈希表类
template <typename T, typename U>
class HashTable
{
public:
HashTable() : vec(hashsize) {}//类中的容器需要通过构造函数来指定大小
~HashTable() {}
bool insert_data(const T &key, const U &value);
int hash(const T &key);
bool hash_find(const T &key);
private:
vector<list<HashNode<T, U>>> vec;//将节点存储到容器中
};
//哈希函数(除留取余)
template <typename T, typename U>
int HashTable<T, U>::hash(const T &key)
{
return key % 13;
}
//哈希查找
template <typename T, typename U>
bool HashTable<T, U>::hash_find(const T &key)
{
int index = hash(key);//计算哈希值
for (auto it = vec[index].begin(); it != vec[index].end(); ++it)
{
if (key == it -> _key)//如果找到则打印其关联值
{
cout << it->_value << endl;//输出数据前应该确认是否包含相应类型
return true;
}
}
return false;
}
//插入数据
template <typename T, typename U>
bool HashTable<T, U>::insert_data(const T &key, const U &value)
{
//初始化数据
HashNode<T, U> node;
node._key = key;
node._value = value;
for (int i = 0; i < hashsize; ++i)
{
if (i == hash(key))//如果溢出则把相应的键值添加进链表
{
vec[i].push_back(node);
return true;
}
}
}
int main(int argc, char const *argv[])
{
HashTable<int, int> ht;
static default_random_engine e;
static uniform_int_distribution<unsigned> u(0, 100);
long long int a = 10000000;
for (long long int i = 0; i < a; ++i)
ht.insert_data(i, u(e));
clock_t start_time = clock();
ht.hash_find(114);
clock_t end_time = clock();
cout << "Running time is: " << static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC * 1000 <<
"ms" << endl;//输出运行时间。
system("pause");
system("pause");
return 0;
}