Hash算法

意義所在

hash算法的意義在於提供了一種快速存取數據的方法,它用一種算法建立鍵值真實值之間的對應關係,(每一個真實值只能有一個鍵值,但是一個鍵值可以對應多個真實值),這樣可以快速在數組等

存取數據

例如:
//HashTable.h
template <class T>
class HashTable
{
public :
HashTable( int count ) ;
void put( T* t ,int key ) ;
T* get( int key ) ;
private :
T** tArray ;
}
//HashTable.cpp
template <Class T>
HashTable::HashTable( int count )
{
tArray = new T*[count] ;
}
template <Class T>
void HashTable::put( T* t , int key )
{
this->tArray[ key ] = t ;
}
template <Class T>
T* HashTable::get( int key )
{
return this->tArray[ key ] ;
}
這樣,我們只要知道key值,就可以快速存取T類型的數據,而不用像在鍊表等數據結構中查找一樣,
要找來找去的.至於key值,一般都是用某種算法(所謂的Hash算法)算出來的.例如:字元串的Hash算法, char* value = "hello"; int key = (((((((27* (int)'h'+27)* (int)'e') + 27)
* (int)'l') + 27) * (int)'l' +27) * 27 ) + (int)'o' ;

相關詞條

相關搜尋

熱門詞條

聯絡我們