C++ 哈希表使用链接、删除方法
C++ Hash Table using chaining, remove method
我正在使用链接在C++中实现哈希表。代码生成时没有错误,并且使用插入方法可以很好地构建表。但是,当我调用 remove 方法时,我收到以下错误:
在 HashTable .exe 中0x00c53be9处出现未经处理的异常: 0xC0000005:访问冲突读取位置0x00000000。
哈希输入代码:
#include <string>
#include <vector>
template <class T>
class HashEntry
{
private:
int key; //lookup key
T value; //hash data
HashEntry<T> *next;
public:
HashEntry(int key, T value);
HashEntry();
int& getKey();
T& getValue();
void setValue(T value);
HashEntry<T>* getNext();
void setNext(HashEntry *next);
bool operator == (HashEntry& rhs);
bool operator != (HashEntry& rhs);
HashEntry<T>& operator = (HashEntry& rhs);
};
template <class T>
HashEntry<T>::HashEntry(int key, T value)
{
this->key = key;
this->value = value;
this->next= nullptr;
}
template <class T>
HashEntry<T>::HashEntry()
{
this->key = 0;
this->next= nullptr;
}
template <class T>
int& HashEntry<T>::getKey()
{
return key;
}
template <class T>
T& HashEntry<T>::getValue()
{
return value;
}
template <class T>
void HashEntry<T>::setValue(T value)
{
this->value = value;
}
template <class T>
HashEntry<T>* HashEntry<T>::getNext()
{
return next;
}
template <class T>
void HashEntry<T>::setNext (HashEntry *next)
{
this->next = next;
}
template <class T>
bool HashEntry<T>::operator == (HashEntry& rhs)
{
return ((this->getKey() == rhs.getKey()) && (this->getValue() == rhs.getValue()));
}
template <class T>
bool HashEntry<T>::operator != (HashEntry& rhs)
{
return ((this->getKey() != rhs.getKey()) && (this->getValue() != rhs.getValue()));
}
template <class T>
HashEntry<T>& HashEntry<T>::operator = (HashEntry& rhs)
{
this->key = rhs.getKey();
this->value = rhs.getValue();
this->next = rhs.getNext();
return *this;
}
哈希表代码:
template <class T>
class HashTable
{
private:
std::vector<HashEntry<T>> table;
static const int DEFAULT_TABLE_SIZE = 128;
int TABLE_SIZE;
public:
HashTable();
void insert(int key, T value);
void remove(int key);
void get(int key);
~HashTable();
};
template <class T>
HashTable<T>::HashTable()
{
TABLE_SIZE = DEFAULT_TABLE_SIZE;
table.resize(TABLE_SIZE);
}
删除方法代码:
template <class T>
void HashTable<T>::remove(int key)
{
int hashFunc = (key % TABLE_SIZE);
if (table[hashFunc] != HashEntry<T>())
{
HashEntry<T> prevEntry = HashEntry<T>();
HashEntry<T> entry = table[hashFunc];
while (entry.getNext() != nullptr && entry.getKey() != key)
{
prevEntry = entry;
entry = *entry.getNext();
}
if (entry.getKey() == key)
{
if (prevEntry == HashEntry<T>())
{
HashEntry<T> nextEntry = *entry.getNext(); //Where the exception is thrown
entry = HashEntry<T>();
table[hashFunc] = nextEntry;
}
else
{
HashEntry<T> *next = entry.getNext();
entry = HashEntry<T>();
prevEntry.setNext(next);
}
}
}
}
while (entry.getNext() != nullptr && entry.getKey() != key)
{
prevEntry = entry;
entry = *entry.getNext();
}
几行之后,使用上面生成的条目:
HashEntry<T> nextEntry = *entry.getNext(); //Where the exception is thrown
while
"makes"entry.getNext(( a nullptr
.而且,稍后,您正在尝试取消引用它。并取消引用nullptr
...是一件坏事(tm(。
顺便说一句,你为什么不对指针进行操作?我可能是错的,但是看着你的代码,我有一种你想修改原始对象的感觉......本地对象看起来像副本。
相关文章:
- 检测win32服务创建和删除的最佳方法
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 一种从内存中删除 UTF 字节的方法?
- 从数组中删除非唯一值、保持顺序和不使用向量的最佳方法?
- 在析构函数中删除单链表的正确方法是什么?
- 处理从列表中删除指向对象的指针的"healthy"方法是什么?
- 删除类成员的动态分配内存的最佳方法是什么
- 从浮点数中删除小数部分但保留类型的有效方法
- 如果方法在类中定义,则阻止编译器删除方法
- 删除复制构造函数的 Intel 13.1.2 中不良C++行为的解决方法
- 通过C++中的删除方法自行删除对象
- 是从我的圆形链接列表中删除方法的定义良好
- 数组放置 新结合_aligned_malloc,正确的删除方法是什么?
- 不能只删除方法的常量重载?
- C++AVLtree删除方法
- 矢量与来自 STL 的列表 - 删除方法
- 带有已删除方法的C++类是否可以简单地复制
- C++ 哈希表使用链接、删除方法
- 成功启用 -fno-finite-math-only on NaN 删除方法
- 二叉搜索树删除方法