插入哈希表

Inserting into a HashTable

本文关键字:哈希表 插入      更新时间:2023-10-16

所以,我真的很困惑。我有一个非常困难的时间试图插入一个项目到哈希表,insert(const Entry& e)我张贴了我最近的尝试下面,我知道它不是很多,但到目前为止,它是我得到的。任何朝着正确方向的推动都是非常感谢的。我的结构和类定义应该没有问题。

当前获取,

hTable。cc:在成员函数' void HT::insert(const Entry&) '中:

hTable。Cc:38:19:错误:没有匹配' operator= '

当我这样做时发生错误,hTable[hashVal] = new Entry(e);

hTable。Cc:37:57: from here

/usr/include/c++/5/bits/predefined_ops.h:234:30: error: expression cannot be

用作函数

{返回bool(_M_pred(*__it));}

第二个错误来自我使用find_if()的同一行,我认为它与我如何使用hashVal有关?我确实查了find_if(),但很多解释对我来说没有多大意义。

下面是表(entry .h)

中的条目的结构
#ifndef H_ENTRY
#define H_ENTRY
#include <string>
using namespace std;
#define ID_SZ      3    // size of key
#define ITEM_SZ    24   // max size for item description 
#define TBL_SZ     31   // default size for hash table
struct Entry {
    string key,   // key
           desc;  // description
    unsigned num; // no of copies
    //constructor
    Entry ( const string& k = "", const string& d = "",
        const unsigned& n = 0 ) : key ( k ), desc ( d ),
        num ( n ) { }
};
#endif

下面是我的哈希表的类定义(hTable.h)

#include "Entry.h"
#ifndef H_HASH_TABLE
#define H_HASH_TABLE
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <list>
#include <vector>
class HT {
public:
    HT ( const unsigned& = TBL_SZ );   // constructor
    ~HT ( );                           // destructor
    void insert ( const Entry& );      // inserts item in hash table
private:
    unsigned hsize;                    // size of hash table
    vector < list < Entry > >  hTable; // hash table
    vector < Entry* > pTable;          // ptr table
    int hash ( const string& );        // hash function
};
#endif

下面是我的hTable。Cc,我的问题在哪里。

#include "hTable.h"
HT::~HT() {
        for (unsigned i = 0; i < hsize; i++)
                        hTable.pop_back();
}
HT::HT(const unsigned& hs) {
        hTable.resize(hs);
        pTable.resize(hs);
}
void HT::insert(const Entry& e) {
//if key already exists, print out message saying so.
//else, insert into hash table
        //also inserts the address of the record (in the hash table) into the pointer table
    int hashVal = hash(e.key);
    if( find_if( hTable.begin(), hTable.end(), hashVal) == hTable.end() )
            hTable[hashVal] = new Entry(e);
    else
            cout << "ntemp messagen";
}

代码中有一些问题:

  1. 如果哈希表不允许重复条目。没有必要将hTable声明为vector < list< Entry > >vector< Entry >满足要求。如果不能更改hTable的声明,则代码为hTable[hashVal].push_back( e );

  2. Entry的复制赋值和复制构造函数需要明确定义

  3. 输出的哈希值需要在哈希表大小的范围内,或者insert函数需要检查哈希值是否在这个范围内

  4. find_if需要一个条件函数或函子。在你的代码中,它应该是。


class CheckKey {
public: 
    CheckKey(const string& key) : _key(hash(key)) {};
    bool operator==(Entry const& e) {
        return hash(e.key) == _key;
    }
private:
    int hash(const string&) {...};
    int _key;
};
void HT::Insert(const Entry& e) { 
    CheckKey chk(e.key);
    vector< list< Entry > >::iterator itfind = find_if(hTable.begin(), hTable.end(), chk);
    if(itfind!=hTable.end() && find->empty()) {
        itfind->push_back(e);
    } else {
        ...
    }
}