哈希表打印和插入功能
Hash table print and insert function
我正在编写一个哈希表函数,它的数据来自一个文本文件。文本文件有一个四位数的学生ID和一个包含空格的名称。问题是insert函数没有从文件中获取第一行。我运行这个程序,当我测试它时,文件中的第一个学生没有出现。我通过输入他们的ID来检查其余学生的姓名,然后他们输出自己的姓名。此外,它不会输出从文件中获得的全名,只输出第一个。如果我从文件中查找chris campos,我会键入它的四位ID,但只有chris会被输出,而不是chris cam波斯。
如果你想看到完整的代码,我会编辑帖子,但我相信错误应该在我发布的代码中。我认为代码是正确的,但我想看看其他人是否能看到错误,或者我如何改进它
此外,我很好奇,我正在使用链接来处理碰撞。我如何让程序输出bucket中所有具有相同四位ID的名称。如果两个名称具有相同的ID,当用户输入ID时,我如何使程序输出这两个名称。
2301 Robb Arredondo
5401 Chris Campos
6305 Yogi Bear
9108 Yoshi Man
0310 John Du
1812 Maria Yu
4318 Power Ranger
7122 Bob Chan
8225 Will Boo
5324 Ghost Lee
0134 Mary Su
2150 Jane Mary
1100 Gary Campos
2305 Alan Kong
3420 Bill Nye
5608 Alex Garcia
9112 Goku Nani
6750 Paul Avalos
1220 Jason Noni
9005 Oscar Roger
6550 Geo Qwerty
1112 Mini Me
2315 Garfield Beria
4201 Just Saying
以下是我的程序如何从文件中获取数据:
HashTable hashtable;
std::ifstream file("students.txt");
int option;
std::string studentID;
std::string studentName;
std::string line;
if (!file.is_open())
{
std::cout << "Error in opening filen";
}
else
{
while (std::getline(file, line))
{
file >> studentID >> studentName;
hashtable.Insert(studentID, studentName);
}
file.close();
}
这是我的插入函数。。
void HashTable::Insert(std::string ID, std::string name)
{
int location = Hash(ID, tableSize);
if (listofStudents[location]->m_idNum == "empty")
{
listofStudents[location]->m_idNum = ID;
listofStudents[location]->m_Name = name;
}
else
{
Student* ptr = listofStudents[location];
Student* newStudent = new Student;
newStudent->m_Name = name;
newStudent->m_idNum = ID;
newStudent->next = NULL;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = newStudent;
}
}
这是我的检索函数。。。
void HashTable::Retrieve(std::string ID)
{
int location = Hash(ID, tableSize);
Student* ptr;
for (ptr = listofStudents[location]; ptr; ptr = ptr->next)
{
if (ptr->m_idNum == ID)
{
std::cout << "--------------------n";
std::cout << "Name of Student: " << ptr->m_Name << std::endl;
std::cout << "---------------------n";
}
else
std::cout << "No Student Foundn";
}
}
如果你想看看哈希技术是如何工作的,这是我的哈希函数。它将字符串转换为int。我使用字符串的原因是因为我需要使用字符串进行赋值。
int HashTable::Hash(const std::string& key, int tablesize)
{
int hashVal = 0;
for (int i = 0; i < key.length(); i++)
{
hashVal = 37 * hashVal + key[i];
}
hashVal %= tablesize;
if (hashVal < 0)
hashVal += tablesize;
return hashVal;
}
罪魁祸首在这里:
file >> studentID >> studentName;
这并不像你想象的那样。我建议将line
转储到std::stringstream
中,然后从中提取字段:
while (std::getline(file, line))
{
std::stringstream ss(line);
std::string ID;
std::string firstName;
std::string lastName;
ss >> ID >> firstName >> lastName;
hashtable.Insert(studentID, firstName + " " + lastName);
}
请注意,您试图将名称转储到一个字符串中,但实际上有两个字段(名字和姓氏),因此需要两个变量。然后你可以把它们组合成一个字符串。
附言:关于哈希表,STL哈希映射实现(std::unordered_map
)是您的朋友。
您的问题在这里:
while (std::getline(file, line))
{
file >> studentID >> studentName; // This only read the id and the first name
hashtable.Insert(studentID, studentName);
}
你应该试试:
file >> studentID >> firstname >> lastname;
file.ignore(); // this will pass the EOL character
相关文章:
- 如何编写用于多映射插入和擦除功能的 API?
- 如何将标准::矢量插入具有自定义排序功能的 std::set 中
- C++插入排序错误功能不起作用
- 插入运算符的过载与使用二传手功能相比
- 使用链表.为什么我的插入功能不起作用?
- 二叉树插入功能不适用于 10 个条目
- 插入功能保持重新创建根节点
- 如何使用字符串更改此插入功能以在链接列表中工作
- 双链接列表插入功能C
- LLVM插入功能调用到另一个函数中
- 创建通用插入功能
- 有什么方法可以将此功能分开并插入空间
- 自定义char*插入功能多次运行时会产生运行时错误
- 哈希表打印和插入功能
- 二叉搜索树在插入功能方面有问题
- C++中的插入器和提取器功能
- 插入功能指向新数据
- QVECTOR插入功能后无法访问点
- GCC vs Clang:将功能与-FPIC一起插入
- 插入功能模板专业化