Hash Table 哈希表 C++ 例子

来源:互联网 发布:嵌入式人工智能 编辑:程序博客网 时间:2024/06/15 08:58

What's a Hash Table? Why we need a Hash Table?


By Using a Hash Table we can find element very quickly. For example, There are 20 random number in an array below.



It's not a sorted array, So We can not use Binary Search to finding a number, When we need to find  118, We need 12 comparisons! Finding number like 270, 198, we need even more comparison.


We change the way the 20 number store. We store them in 10 linked lists. There is a rule, If the number's last digit is 0, so we insert it in a  linked list which  index is 0. Look at  the Figure 1 for more detail. like number 118, it's last digit is 8, so we  insert it in ninth linked list.



So in this way, We find 118 only need 1 comparison in the ninth linked list! Finding number like 270 or 198, we need just 2 comparisons.


Hash Function


Note that the basic hash table which we explained above used a modulo function. There are many way to hash data, but modulo is the most common. What's more, the modulo function often use a prime number. That was because you get fewer collisions when you modulo a key  by a prime number. Having fewer collisions makes your table easier to work and more efficient. There is a completed mathmatical reasoning behind this, but it is okay for us to assume that this is true for us.


A Hash Table Class Example


You need have some STD knowledge, We will use "list" in STD instead of using customied linked list. And we also use  "find" and "erase" function.


#ifndef _HASHTABLE_#define _HASHTABLE_#include <list>#include <algorithm>using namespace std;class HashTable{private:list<int> containers[10];int HashFunction(const int& v) const;int count;public:HashTable();~HashTable();void Insert(const int& e);bool Find(const int& e) const;bool Delete(const int& e);int Count() const;};int HashTable::HashFunction(const int& v) const{return v%10;}HashTable::HashTable(){count = 0;}HashTable::~HashTable(){count = 0;for(int i = 0; i< 10; ++i){containers[i].clear();}}void HashTable::Insert(const int& e){const int hashResult = HashFunction(e);containers[hashResult].push_back(e);++count;}bool HashTable::Find(const int& e) const{const int hashResult = HashFunction(e);list<int>::const_iterator itr = find(containers[hashResult].begin(),containers[hashResult].end(), e);if(itr != containers[hashResult].end()){return true;}else{return  false ;}}bool HashTable::Delete(const int& e){const int hashResult = HashFunction(e);list<int>::iterator itr = find(containers[hashResult].begin(),containers[hashResult].end(), e);if(itr != containers[hashResult].end()){containers[hashResult].erase(itr);--count;return true;}else{return false;}}int HashTable::Count()const{return count;}#endif

Test the simple hash table class


// SimpleHashTable.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "HashTable.h"#include <iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){HashTable h;h.Insert(234);h.Insert(567);h.Insert(987);h.Insert(222);h.Insert(564);h.Insert(111);h.Delete(234);cout << boolalpha;cout << "Is 234 exist? " << h.Find(234)  << endl;cout << "Is 111 exist? " << h.Find(111) << endl;cout << "Total count:" << h.Count() << endl;int i;cin >> i;return 0;}



A more completed Hash Table class


#ifndef _HASHTABLE_#define _HASHTABLE_#include <list>#include <string>#include <algorithm>using namespace std;class Entry{private:int m_key;string m_data;public:friend bool operator == (const Entry& e1,const Entry& e2);Entry(){}Entry(const int& key,const string& data){m_key = key;m_data = data;}void setKey(const int& key){m_key = key;}void setData(const string& data){m_data = data;}int getKey()const{return m_key;}string getData()const{return m_data;}};bool operator ==(const Entry& e1,const Entry& e2){return (e1.m_key == e2.m_key);}class HashTable{private:list<Entry> containers[10];int HashFunction(const int& v) const;int count;public:HashTable();~HashTable();void Insert(const Entry& entry);bool Find(const int& key,list<Entry>::const_iterator& itr) const;bool Delete(const int& key);int Count() const;};int HashTable::HashFunction(const int& v) const{return v%10;}HashTable::HashTable(){count = 0;}HashTable::~HashTable(){count = 0;for(int i = 0; i< 10; ++i){containers[i].clear();}}void HashTable::Insert(const Entry& entry){const int hashResult = HashFunction(entry.getKey());containers[hashResult].push_back(entry);++count;}bool HashTable::Find(const int& key,list<Entry>::const_iterator& out) const{const int hashResult = HashFunction(key);Entry entry;entry.setKey(key);list<Entry>::const_iterator itr = find(containers[hashResult].begin(),containers[hashResult].end(), entry);if(itr != containers[hashResult].end()){         out = itr; return true;}else{return  false ;}}bool HashTable::Delete(const int& key){const int hashResult = HashFunction(key);Entry entry;    entry.setKey(key);list<Entry>::iterator itr = find(containers[hashResult].begin(),containers[hashResult].end(), entry);if(itr != containers[hashResult].end()){containers[hashResult].erase(itr);--count;return true;}else{return false;}}int HashTable::Count()const{return count;}#endif

Test the completed hash table class


// HashTableApp.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "HashTable.h"#include <iostream>#include <string>using namespace std;int _tmain(int argc, _TCHAR* argv[]){HashTable h;h.Insert(Entry(234,"one"));h.Insert(Entry(567,"two"));h.Insert(Entry(987,"three"));h.Insert(Entry(222,"four"));h.Insert(Entry(564,"five"));h.Insert(Entry(111,"six"));h.Delete(234);list<Entry>::const_iterator itr;if(h.Find(234,itr)){cout <<"The data with key 234: " << itr->getData() << endl;}else{cout << "Can not find the data with key 234 " << endl;}if(h.Find(111,itr)){cout <<"The data with key 111: " << itr->getData() << endl;}else{cout << "Can not find the data with key 111" << endl;}cout << "Total count: " << h.Count() << endl;int i;cin >> i;return 0;}

http://www.waitingfy.com/?p=480


原创粉丝点击