Hash Table 哈希表 C++ 例子
2013-07-16 14:24
441 查看
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 getfewer 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.[cpp]
view plaincopyprint?
#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
#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
[cpp]view plaincopyprint?
// 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;
}
// 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
[cpp]view plaincopyprint?
#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
#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
[cpp]view plaincopyprint?
// 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;
}
// 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
相关文章推荐
- Hash Table 哈希表 C++ 例子
- Hash Table 哈希表 C++ 例子
- C++数据结构 之 哈希表(散列表)_Hash Table
- C/C++ 实现哈希表(hash table)
- 一个哈希表Hash Table的例子,用于IPv4协议的IP地址查找
- C++ 哈希表 线性探测 二次探测 哈希桶
- C++继承经典例子
- C++例子:setprecision
- 《数据结构》中的哈希表实现的例子(C语言)
- 【ThinkingInC++】45、比较const和非const成员函数的例子
- 2009-08-27 22:21 C++调试时的错误例子及解决方法
- C++ 打印文件名, 行号,编译日期和时间例子
- c++编写的多线程售票的例子
- C++ 运算符重载 一个例子
- c++自定义消息完整例子
- C++学习------包含多态的例子
- Hash Table 哈希表介绍
- nodejs调用c++ addon的简单例子(带参数)
- C/C++与Lua交互(C实现的Lua编译器的例子)
- Active Object 中的 C++例子