首页 > 编程语言 > 详细

HashTable 哈希表 C++

时间:2014-03-17 10:45:49      阅读:524      评论:0      收藏:0      [点我收藏+]

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.

bubuko.com,布布扣

 

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.

 

bubuko.com,布布扣

 

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.

 

C++ Code of Hash Table

bubuko.com,布布扣
/* Class : HastTable
 * Function : A simple Hash table class
 * Author : Jason
 * Date : 2014.03.16
 */
class HashTable
{
public:
    typedef list<int>::iterator ListIter;
private:
    list<int> m_Container[10];
    int HashFunction(const int& v) const;
    int m_Count;
public:
    HashTable();
    ~HashTable();
    void Insert(const int& v);
    bool Find(const int& v);
    bool Delete(const int& v);    
    int Count() const;
};

HashTable::HashTable()
{
    m_Count = 0;
}

HashTable::~HashTable()
{
    m_Count = 0;
    for (int i = 0; i < 10; i++)
    {
        m_Container[i].clear();
    }
}

// This Hash Function is very simple
int HashTable::HashFunction(const int& v) const
{
    return v%10;
}

int HashTable::Count() const
{
    return m_Count;
}

void HashTable::Insert(const int& v)
{
    int hashRes = HashFunction(v);
    m_Container[hashRes].push_back(v);
    ++m_Count;
}

bool HashTable::Find(const int& v)
{
    int hashRes = HashFunction(v);
    //typedef list<int>::iterator ListIter;
    //ListIter FindIter = find(m_Container[hashRes].begin(), m_Container[hashRes].end(), v);
    //if (FindIter != m_Container[hashRes].end())
    //{
    //    return true;
    //} 
    //else
    //{
    //    return false;
    //}
    for (ListIter FindIter = m_Container[hashRes].begin(); FindIter != m_Container[hashRes].end(); ++FindIter)
    {
        if (*FindIter == v)
        {
            return true;
        }
    }
    return false;
}

bool HashTable::Delete(const int& v)
{
    int hashRes = HashFunction(v);
    //ListIter DelIter = find(m_Container[hashRes].begin(), m_Container[hashRes].end(), v);
    //if (DelIter != m_Container[hashRes].end())
    //{
    //    m_Container[hashRes].erase(DelIter);
    //    --m_Count;
    //    return true;
    //}
    //return false;
    for (ListIter DelIter = m_Container[hashRes].begin(); DelIter != m_Container[hashRes].end(); ++DelIter)
    {
        if (*DelIter == v)
        {
            m_Container[hashRes].erase(DelIter);
            m_Count--;
            return true;
        } 
    }
    return false;
}
bubuko.com,布布扣

下面是是一个稍微复杂一些的C++版本的代码

bubuko.com,布布扣
/* Class : HastTable
 * Function : A completed Hash table class
 * Author : Jason
 * Date : 2014.03.16
 */
class Entry
{
private:
    int m_nKey;
    string m_strData;
public:
    friend bool operator==(const Entry& E1, const Entry& E2);
    Entry();
    Entry(const int& nKey, const string& strData);
    ~Entry();
    void SetKey(int nKey);
    void SetData(const string& strData);
    int GetKey() const;
    string GetData() const;
};

bool operator==(const Entry& E1, const Entry& E2)
{
    return E1.m_nKey == E2.m_nKey ? true : false;
}

Entry::Entry()
{}
Entry::Entry(const int& nKey, const string& strData)
{
    m_nKey = nKey;
    m_strData = strData;
}
Entry::~Entry()
{
    m_nKey = 0;
    m_strData = \0;
}
void Entry::SetKey(int nKey)
{
    m_nKey = nKey;
}
void Entry::SetData(const string& strData)
{
    m_strData = strData;
}
int Entry::GetKey() const
{
    return m_nKey;
}
string Entry::GetData() const
{
    return m_strData;
}

class EntryHashTable
{
public:
    typedef list<Entry>::const_iterator ListEntryIter;
private:
    list<Entry> m_Container[10];
    int HashFunction(const int& entry) const;
    int m_Count;
public:
    EntryHashTable();
    ~EntryHashTable();
    void Insert(const Entry& entry);
    bool Find(const Entry& entry) const;
    bool Delete(const Entry& entry);
    int Count() const;
};

int EntryHashTable::HashFunction(const int& v) const
{
    return v%10;
}

EntryHashTable::EntryHashTable()
{
    
}

EntryHashTable::~EntryHashTable()
{
    for (int i = 0; i < 10; i++)
    {
        m_Container[i].clear();
    }
    m_Count = 0;
}

void EntryHashTable::Insert(const Entry& entry)
{
    int HashRes = HashFunction(entry.GetKey());
    m_Container[HashRes].push_back(entry);
    ++m_Count;
}

bool EntryHashTable::Find(const Entry& entry) const
{
    int HashRes = HashFunction(entry.GetKey());
    ListEntryIter FindIter;
    for (FindIter = m_Container[HashRes].begin(); FindIter != m_Container[HashRes].end(); ++FindIter)
    {
        if (*FindIter == entry)
        {
            return true;
        }
    }
    return false;
}

bool EntryHashTable::Delete(const Entry& entry)
{
    int HashRes = HashFunction(entry.GetKey());
    ListEntryIter DelIter;
    for (DelIter = m_Container[HashRes].begin(); DelIter != m_Container[HashRes].end(); ++DelIter)
    {
        if (*DelIter == entry)
        {
            m_Container[HashRes].erase(entry);
            --m_Count;
            return true;
        }
    }
    return false;
}

int EntryHashTable::Count() const
{
    return m_Count;
}
bubuko.com,布布扣

HashTable 哈希表 C++,布布扣,bubuko.com

HashTable 哈希表 C++

原文:http://www.cnblogs.com/Jasonscor/p/3604336.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!