C++ Person对象c的向量哈希表
我一直在为我的一个班级做一个非常深入的项目。它应该读取个人对象并将其放入哈希表中。我仍在努力了解哈希表的概念,希望能得到任何帮助。 这将是基于姓氏的散列,因为有些人可能有相同的姓氏,我打算让每个bucket成为Person对象的向量。我试图通过向散列函数添加一个人然后返回来测试这个类。我的代码编译成功,但此行的put函数中出现线程错误:table[index]。push_back(p) 谁能帮我弄清楚出了什么问题吗?谢谢大家!C++ Person对象c的向量哈希表,c++,object,hash,vector,hashtable,C++,Object,Hash,Vector,Hashtable,我一直在为我的一个班级做一个非常深入的项目。它应该读取个人对象并将其放入哈希表中。我仍在努力了解哈希表的概念,希望能得到任何帮助。 这将是基于姓氏的散列,因为有些人可能有相同的姓氏,我打算让每个bucket成为Person对象的向量。我试图通过向散列函数添加一个人然后返回来测试这个类。我的代码编译成功,但此行的put函数中出现线程错误:table[index]。push_back(p) 谁能帮我弄清楚出了什么问题吗?谢谢大家! int main() { HashTable ht(10);
int main()
{
HashTable ht(10);
ht.put(p1, p1->lName);
ht.getName("Booras");
}
HashTable:
#include "Person.h"
#include <vector>
class HashTable: public DataStructures
{
private:
vector<vector<Person>> table;
public:
HashTable(int tableSize);
~HashTable();
int tableSize;
void getName(string str); //prints out friends with matching name
void put(Person p, string str);
void remove(Person *p, string str);
int hash(string str);
};
HashTable::HashTable(int tableSize)
{
vector< vector<Person> > table(tableSize, vector<Person>(tableSize));
for (int i = 0; i < tableSize; i++) {
table.push_back(vector<Person>()); // Add an empty row
}
}
HashTable::~HashTable()
{
}
//Find a person with the given last name
void HashTable::getName(string key)
{
int index = hash(key);
for(int i=0; i<table[index].size(); i++)
{
if(table[index][i].lName.compare(key) == 0)
std::cout << "Bucket: " << index << "Bin: " << i;
table[index][i].print();
}
//create exception for person not found
}
void HashTable::put(Person p, string str)
{
int index = hash(str);
table[index].push_back(p);
}
void HashTable::remove(Person *p, string str)
{
int index = hash(str);
int i=0;
while(&table[index][i] != p && i<table[index].size())
i++;
for(int j=i; j<table[index].size()-1; j++)
table[index][j] = table[index][j+1];
table[index].pop_back();
}
int HashTable::hash(string str)
{
int hashValue = 0;
for(int i=0; i<str.length(); i++)
{
hashValue = hashValue + int(str[i]);
}
hashValue %= tableSize;
if(hashValue<0) hashValue += tableSize;
return hashValue;
}
您没有向我们展示
HashTable::hash(string)
member函数,但我假设您的问题源自HashTable
构造函数:您没有初始化tableSize
成员变量,您需要计算有效的哈希索引
在查看构造函数时:
HashTable::HashTable(int tableSize)
{
vector< vector<Person> > table(tableSize, vector<Person>(tableSize));
现在您已经添加了更多的行,因此table.size()==2*tableSize
,其中前半部分条目为非空(如上所述),后半部分包含空向量
那可能不是你想要的
在所有这些中,您还没有初始化成员
tableSize
。如果您使用隐藏成员名称的局部变量或参数名称,则很容易混淆。您可以发布HashTable::hash
成员函数的代码吗?顺便问一下:您的main()
如何编译?p1
是指针(那么你就不能像这样调用put
)还是Person
对象(那么p1->lName
应该不能工作,除非你意外地使该操作符超负荷)?刚刚发布了hash函数和我的一部分主函数,该函数用于创建哈希表。那么我如何构建构造函数来创建Person对象的tableSize空向量的哈希表呢?HashTable::HashTable(int tableSize):table(tableSize),tableSize(tableSize){}
将是一个完整的构造函数,可以按照您的意愿运行。或者为了消除命名混乱:哈希表::哈希表(int bucketCount):表(bucketCount),表大小(bucketCount){}
非常感谢您的帮助!我真的很感激
HashTable::HashTable(int tableSize)
{
vector< vector<Person> > table(tableSize, vector<Person>(tableSize));
for (int i = 0; i < tableSize; i++) {
table.push_back(vector<Person>()); // Add an empty row
}
}