C ++ Hashtable问题

时间:2011-04-19 10:37:14

标签: c++ hashtable

我想尝试在c ++中使用Hastable来存储字符串,即字符串键和数据。我正在使用2003年的书籍数据结构中的代码为游戏程序员。

当我尝试使用字符串1执行相同操作时,我实现了一个坚持错误的int hastable im。

c ++一直给我同样的错误;

//code
#include <string>
#include <iostream>
#include "HashTable.h"

using namespace std;


//hash table
//***********************************************************************
unsigned long int Hash( int i )
{
    return i;
}

unsigned long int StringHash( const char* p_string )
{
    unsigned long int hash = 0;
    int i;
    int length = strlen( p_string );
    for( i = 0; i < length; i++ )
    {
        hash += ( (i + 1) * p_string[i] );
    }
    return hash;
}

HashTable<string, string> table( 10, StringHash ); <<--problem here ERROR BELOW
HashEntry<string, string>* entry;
//***********************************************************************
  

错误6错误C2664:   “哈希表::哈希表(整型,无符号   long(__ cdecl *)(KeyType))':不能   从'unsigned转换参数2   long(__cdecl *)(const char *)'to   'unsigned long(__cdecl   *)(KeyType)'j:\ my_docs \ college 2010 -2011 \游戏数据结构\ ca3_datastructures_gerry mc   donnell \ ca3_datastructures_gerry mc   donnell \ loginuser.h 31 CA3_DataStructures_gerry   mc donnell

如果有人能提供帮助就会很棒。

本书提供了stringhash函数,所以我不确定我做错了什么。

Hashtable.h

// ============================================================================
// Data Structures For Game Programmers
// Ron Penton
// HashTable.h
// This file holds the Linekd Hash Table implementation.
// ============================================================================


#ifndef HASHTABLE_H
#define HASHTABLE_H

#include "DLinkedList.h"
#include "Array.h"


// -------------------------------------------------------
// Name:        HashEntry
// Description: This is the hash table entry class. It
//              stores a key and data pair.
// -------------------------------------------------------
template< class KeyType, class DataType >
class HashEntry
{
public:
    KeyType m_key;
    DataType m_data;
};



// -------------------------------------------------------
// Name:        HashTable
// Description: This is the hashtable class.
// -------------------------------------------------------
template< class KeyType, class DataType >
class HashTable
{
public:

    // typedef the entry class to make is easier to work with.
    typedef HashEntry<KeyType, DataType> Entry;


// ----------------------------------------------------------------
//  Name:           HashTable
//  Description:    construct the table with a size, and a hash 
//                  function. The constructor will construct the 
//                  m_table array with the correct size.
//  Arguments:      p_size: The size of the table
//                  p_hash: the hashing function.
//  Return Value:   None
// ----------------------------------------------------------------
    HashTable( int p_size, unsigned long int (*p_hash)(KeyType) )
        : m_table( p_size )
    {
        // set the size, hash function, and count.
        m_size = p_size;
        m_hash = p_hash;
        m_count = 0;
    }


// ----------------------------------------------------------------
//  Name:           Insert
//  Description:    Inserts a new key/data pair into the table. 
//  Arguments:      p_key: the key
//                  p_data: the data attached to the key.
//  Return Value:   None
// ----------------------------------------------------------------
    void Insert( KeyType p_key, DataType p_data )
    {
        // create an entry structure.
        Entry entry;
        entry.m_data = p_data;
        entry.m_key = p_key;

        // get the hash value from the key, and modulo it
        // so that it fits within the table.
        int index = m_hash( p_key ) % m_size;

        // add the entry to the correct index, increment the count.
        m_table[index].Append( entry );
        m_count++;
    }


// ----------------------------------------------------------------
//  Name:           Find
//  Description:    Finds a key in the table
//  Arguments:      p_key: the key to search for
//  Return Value:   a pointer to the entry that has the key/data,
//                  or 0 if not found.
// ----------------------------------------------------------------
    Entry* Find( KeyType p_key )
    {
        // find out which index the key should exist in
        int index = m_hash( p_key ) % m_size;

        // get an iterator for the list in that index.
        DListIterator<Entry> itr = m_table[index].GetIterator();

        // search each item
        while( itr.Valid() )
        {
            // if the keys match, then return a pointer to the entry
            if( itr.Item().m_key == p_key )
                return &(itr.Item());
            itr.Forth();
        }

        // no match was found, return 0.
        return 0;
    }


// ----------------------------------------------------------------
//  Name:           Remove
//  Description:    Removes an entry based on key 
//  Arguments:      p_key: the key
//  Return Value:   true if removed, false if not found.
// ----------------------------------------------------------------
    bool Remove( KeyType p_key )
    {
        // find the index that the key should be in.
        int index = m_hash( p_key ) % m_size;

        // get an iterator for the list in that index.
        DListIterator<Entry> itr = m_table[index].GetIterator();

        // search each item
        while( itr.Valid() )
        {
            // if the keys match, then remove the node, and return true.
            if( itr.Item().m_key == p_key )
            {
                m_table[index].Remove( itr );
                m_count--;
                return true;
            }
            itr.Forth();
        }

        // item wasn't found, return false.
        return false;
    }


// ----------------------------------------------------------------
//  Name:           Count
//  Description:    Gets the number of entries in the table.
//  Arguments:      None
//  Return Value:   Number of entries in the table.
// ----------------------------------------------------------------
    int Count()
    {
        return m_count;
    }


// ----------------------------------------------------------------
//  Name:           m_size
//  Description:    This is the size of the table
// ----------------------------------------------------------------
    int m_size;

// ----------------------------------------------------------------
//  Name:           m_count
//  Description:    This is the number of entries in the table.
// ----------------------------------------------------------------
    int m_count;

// ----------------------------------------------------------------
//  Name:           m_table
//  Description:    This is the actual table, a list of linked 
//                  lists of entries.
// ----------------------------------------------------------------
    Array< DLinkedList< Entry > > m_table;

// ----------------------------------------------------------------
//  Name:           m_hash
//  Description:    a pointer to the hash function.
// ----------------------------------------------------------------
    unsigned long int (*m_hash)(KeyType);


};


#endif

// array.h

3 个答案:

答案 0 :(得分:3)

您的Hashtable构造函数需要哈希函数来获取KeyType参数。

您正在创建一个Hashtable std::string作为KeyType,但是传递一个带有char*参数的哈希函数。那些不兼容。

使用char*密钥类型创建哈希,或者更改哈希函数以使用std::string

答案 1 :(得分:0)

HashTable.h中,哈希函数具有函数签名unsigned long int (*p_hash)(KeyType)。也就是说,如果您的哈希表是HashTable<string, string>,则哈希函数的预期形式为unsigned long int (*p_hash)(string),而不是unsigned long int (*p_hash)(const char*)

答案 2 :(得分:0)

HashTable<string, string> table( 10, StringHash );

string(第二个模板参数)是您的KeyType,但您的哈希函数需要const char *

无论

unsigned long int StringHash( const string & p_string )

 HashTable<string, const char *> table( 10, StringHash );

应该有效。 (尚未编译或测试)