插入哈希表
Inserting into a HashTable
所以,我真的很困惑。我有一个非常困难的时间试图插入一个项目到哈希表,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";
}
代码中有一些问题:
-
如果哈希表不允许重复条目。没有必要将
hTable
声明为vector < list< Entry > >
。vector< Entry >
满足要求。如果不能更改hTable
的声明,则代码为hTable[hashVal].push_back( e );
-
Entry
的复制赋值和复制构造函数需要明确定义 -
输出的哈希值需要在哈希表大小的范围内,或者
insert
函数需要检查哈希值是否在这个范围内 -
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 {
...
}
}
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- C++中的并发哈希表
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 如果索引不是整数,我们如何在 C++ 中插入哈希表
- 在 C++ 中为特定哈希表创建插入函数
- 通过线性探测插入哈希表
- 哈希表打印和插入功能
- 在C++中实现哈希表(插入和延迟删除)
- 将新元素插入哈希表
- C++哈希表插入功能
- 缩短哈希表的插入时间 - C++
- 插入哈希表
- 哈希表在增加大小后重新插入
- 实现哈希表,插入函数导致"scope"错误
- 将typedef map插入哈希表
- 正在将对象插入哈希表...出于某种原因不让我?
- 在哈希表中插入一个struct对象