我有以下input.txt文件,实际上有大约150k ID。
id element
0 1
0 3
1 0
1 1
1 3
2 2
2 4
3 4
4 1
我想将id作为键和值存储在一个包含每个id的值的向量中。例如,最后我想要一个看起来像这样的哈希表。
0 -> 1, 3
1 -> 0, 1, 3
2 -> 2, 4
3 -> 4
4 -> 1
到目前为止,这是我的代码:
#include<iostream>
#include<cstdlib>
#include<vector>
using namespace std;
const int TABLE_SIZE = 128;
class HashEntry
{
public:
int key;
vector< int > values;
HashEntry(int key, int value)
{
this->key = key;
values.push_back(value);
}
};
class HashMap
{
private:
HashEntry **table;
public:
HashMap()
{
table = new HashEntry * [TABLE_SIZE];
for (int i = 0; i< TABLE_SIZE; i++)
{
table[i] = NULL;
}
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_SIZE;
}
void Insert(int key, int value)
{
int hash = HashFunc(key);
table[hash] = new HashEntry(key, value);
}
void Show(){
for (int i=0;i<TABLE_SIZE;i++){
if (table[i]!=NULL){
cout << table[i]->key << " : ";
for(int y = 0; y < table[i]->values.size(); y++) {
cout << table[i]->values[y];
}
cout << endl;
}
}
}
};
int main()
{
HashMap hash;
int key, value;
while (1)
{
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
hash.Show();
}
return 0;
}
在控制台中,它只显示我输入的最后一个元素,而不是所有值。我认为问题是HashEntry对象每次都被初始化,并且每次都从头开始初始化它的成员。如何保留HashEntries&#34;静态&#34;?
编辑: 按照Davide Spataro的建议,这是我的新代码。不幸的是我无法使用C ++ 11所以我稍微改了一下。现在出了什么问题?
void Insert(int key, int value)
{
int hash = HashFunc(key);
//check if we already have an `HashEntry` for key
HashEntry *p = find(key, table[hash]);
// if yes simply push the value in that `HashEntry`
if( p->key == -1 ){
p->values.push_back(value);
return;
}
else{
//otherwise add an `HashEntry` to the hashtable for key and push a value in it.
HashEntry *p = new HashEntry(key, value);
p->values.push_back(value);
table[hash] = p;
}
}
HashEntry* find(int key, HashEntry *table)
{
for (int i=0;i<TABLE_SIZE;i++)
{
if (key == table[i].key)
return &table[i];
else return new HashEntry(-1,-1);
}
}
答案 0 :(得分:0)
问题主要出在Insert
。每当你找到一个特定值的哈希值时,你就会覆盖可能已经创建一个新值的HashEntry
!这是错误的,也会导致内存泄漏。
您还需要处理碰撞。您的哈希表中只有128
个桶和150k个可能的键值。对于鸽子原则,对于相同的哈希值,最终会有多个HashEntry
。您的代码需要处理此问题。
以下内容应该足以作为起点。
void Insert(int key, int value)
{
int hash = HashFunc(key);
//check if we already have an `HashEntry` for key
auto p = find(key, table[hash]);
// if yes simply push the value in that `HashEntry`
if( p != nullptr){
p->values.push_back(value);
return;
}else{
//otherwise add an `HashEntry` to the hashtable for key and push a value in it.
auto p = new HashEntry(key, value);
p->values.push_back(value);
table[hash] = p;
}
}
还有其他问题,例如评论中提到的rule 3/5/0,因为您管理原始指针。 (你需要在某个时候释放那个记忆,不是吗?)