C++ C++;具有resize的哈希表插入方法不起作用?
假设此方法在阈值超过预定量0.65时调整数组大小。 问题是,调整大小后,析构函数将删除包含所有新复制信息的数组。我不知道怎么修理它 关于这方面的一些评论只是我在头脑风暴,思考问题可能是什么C++ C++;具有resize的哈希表插入方法不起作用?,c++,hashtable,probing,linear-probing,quadratic-probing,C++,Hashtable,Probing,Linear Probing,Quadratic Probing,假设此方法在阈值超过预定量0.65时调整数组大小。 问题是,调整大小后,析构函数将删除包含所有新复制信息的数组。我不知道怎么修理它 关于这方面的一些评论只是我在头脑风暴,思考问题可能是什么 void Hashtable::insert(int value) { int i = 0; int key = hash(value); // hash(value) % N? //check load factor to see if it needs to be resiz
void Hashtable::insert(int value)
{
int i = 0;
int key = hash(value); // hash(value) % N?
//check load factor to see if it needs to be resized
double q = ((double)(currsize+1) / (double)currcapacity); //calculate current threshold
if ( q >= threshhold) { //if current threshold is >= to 0.65
int newSize = nextPrime(currcapacity * 2); //get new size at the next prime number
Hashtable newtable(newSize); //create a new table; HOW DO I CREATE THIS ON THE HEAP?
for (int j = 0; j < currcapacity; j++) { //runs through table calling insert to insert new items into table
if (htable[j] != EmptyBeforeStart && htable[j] != EmptyAfterRemoval) {
newtable.insert(htable[j]);
}
}
delete[] htable; //delete old table
this->htable = newtable.htable; //re-assign address of htbale to be newtable.htable //THIS ASSINGMENTS GETS DELETED BY THE DESTRUCTOR
this->currcapacity = newSize; //update capacity
this->insert(value);
//THE PROBLEM IS THAT THE DESTRUCTOR GETS CALLED AND DELETES THE htable BECAUSE THE NEWTABLE HTABLE WAS DECLARED AUTOMAITCALLY NOT ON THE HEAP.
}
else {
while (i < currcapacity) {
if (htable[key] == EmptyBeforeStart || htable[key] == EmptyAfterRemoval) {
htable[key] = value;
currsize++;
return;
}
i++;
key = (hash(value) + 0 * i + 1 * i * i) % (int)currcapacity;
}
}
}
void哈希表::插入(int值)
{
int i=0;
int key=hash(value);//hash(value)%N?
//检查负载系数以查看是否需要调整其大小
双q=((双)(currsize+1)/(双)currcapacity);//计算当前阈值
如果(q>=阈值){//如果当前阈值>=到0.65
int newSize=nextTime(currcapacity*2);//在下一个素数处获取新大小
Hashtable newtable(newSize);//创建一个新表;如何在堆上创建它?
for(int j=0;jhtable=newtable.htable;//将htbale的地址重新分配为newtable.htable//析构函数将删除此分配
此->currcapacity=newSize;//更新容量
此->插入(值);
//问题是析构函数被调用并删除htable,因为NEWTABLE htable是自动声明的,不在堆上。
}
否则{
而(i<当前容量){
如果(htable[key]==重新启动前清空| | htable[key]==删除后清空){
htable[键]=值;
currsize++;
返回;
}
i++;
key=(散列(值)+0*i+1*i*i)%(int)当前容量;
}
}
}
调用析构函数是因为您使用此行在堆栈上创建了一个哈希表
对象:
Hashtable newtable(newSize);
这创建了一个临时表,您填充了该表,然后在函数返回之前将其丢弃(销毁)。但在此之前,您窃取了它的指针并将其存储在当前哈希表中。这是一个大问题,因为当该表被销毁时,它将尝试删除一个已经被删除的指针
我想你的意思是:
int *newtable = new int[newSize];
std::fill(newtable, newtable + newSize, EmptyBeforeStart);
但是,您需要更改将现有值复制到此文件的重新灰化部分。现在,我并不是说以下是一个伟大的设计,但不需要修改任何其他内容,您就可以做到这一点:
// Store information about current table before replacing it
int *oldtable = htable;
int oldcapacity = currcapacity;
// Replace the hash table with new empty one
htable = newtable;
currcapacity = newSize;
// Rehash old values
for (int j = 0; j < oldcapacity ; j++) {
if (oldtable[j] != EmptyBeforeStart && oldtable[j] != EmptyAfterRemoval) {
insert(oldtable[j]);
}
}
// Clean up
delete[] oldtable;
//在替换当前表之前存储有关该表的信息
int*oldtable=htable;
int oldcapacity=电流容量;
//将哈希表替换为新的空哈希表
htable=新表;
currcapacity=新闻大小;
//重提旧价值观
对于(int j=0;j
看看上面的代码实际上是如何用新的空表替换表的,但保留了一个指向旧数据的指针。然后调用insert
,将所有旧值散列到新表中。然后,当它完成时,旧内存可以被释放