哈希类哈希
Hash Class Hashing
我为学校的作业创建了一个哈希类,该作业实现了哈希并处理了冲突。 当通过上传 25 个项目(学生记录)来测试这个类时,我注意到我的哈希类只是将每个项目按顺序插入到用于存储的数组中(而不是使用哈希代码)。 但是,在调试模式下,insertItemCollision() 函数可以正确计算位置,但项目是按顺序插入的。
是什么原因导致这种情况发生,如何解决? 谢谢!
哈希.h
//hash.h
#include <string>
struct Student
{
std::string name;
std::string id;
};
class MyHash{
public:
MyHash();
int hashCode(int, int);
void insertItemCollision(std::string, std::string);
std::string retrieveItem(std::string);
Student students[100];
};
哈希.cpp
//hash.cpp
#include <iostream>
#include "Hash.h"
MyHash::MyHash()
{
}
int MyHash::hashCode(int id, int max)
{
return (id % max);
}
void MyHash::insertItemCollision(std::string id, std::string name)
{
int idInt = atoi(id.c_str());
int location;
location = hashCode(idInt, 100);
while (students[location].id != "")
location = (location + 1) % 100;
students[location].id = id;
students[location].name = name;
}
std::string MyHash::retrieveItem(std::string id)
{
int location;
int startLoc;
int idInt = atoi(id.c_str());
bool moreToSearch = true;
bool found;
std::string item;
startLoc = hashCode(idInt, 100);
location = startLoc;
do
{
if (students[location].id == id || students[location].id == "")
moreToSearch = false;
else
location = (location + 1) % 100;
} while (location != startLoc && moreToSearch);
found = (students[location].id == id);
if (found)
item = students[location].name;
return item;
}
学生.txt
//students.txt
9892 Zack Lewis
4592 Ken Rodriguez
9819 Anderson Clark
1519 Ben Robinson
4597 Abigail Martinez
8542 Madison Garcia
6113 Mia Thompson
8591 Chloe Martin
9491 Daniel Harris
1698 Aiden White
5984 Alexander Walker
6541 Ethan Jackson
9549 Michael Thomas
5949 Emily Anderson
9861 Ava Taylor
5412 Noah Moore
6262 Olivia Wilson
1954 Jayden Miller
4954 William Davis
9567 Emma Brown
5195 Mason Jones
9195 Isabella Williams
5199 Sophia Johnson
1294 Jacob Smith
驱动程序.cpp
//driver.cpp
#include <iostream>
#include<string>
#include<fstream>
#include "Hash.h"
using namespace std;
int read(string[]);
void splitString(string, Student&);
void init(string[], MyHash*, int);
int showMenu();
int main()
{
int size;
int choice;
string input[100];
MyHash* h = new MyHash();
size = read(input);
init(input, h, size);
do
{
choice = showMenu();
if (choice == 1)
{
string id;
cout << "Enter the id of the sutdent you would like to find: " << endl;
cin >> id;
std::string s = (*h).retrieveItem(id);
if (s != "")
cout << "The students name is: "<< s << endl;
else
cout << "No students matching that id was found!" <<endl;
}
}while (choice != 2);
system("pause");
return 0;
}
int read(string st[])
{
int size = 0;
ifstream infilestream;
infilestream.open("test.txt");
for(int i = 0; infilestream.good(); i++)
{
getline(infilestream, st[i]);
cout<<st[i] <<endl;
size++;
}
infilestream.close();
return size;
}
void splitString(string record, Student& s)
{
s.id = record.substr(0, 4);
s.name = record.substr(5, record.length());
}
void init(string inputs[], MyHash* myHash, int size)
{
for(int i = 0;i < size; i++)
{
splitString(inputs[i],myHash->students[i]);
//cout << stus[i].name << " " << stus[i].id << endl;
myHash->insertItemCollision(myHash->students[i].id, myHash->students[i].name);
}
}
int showMenu()
{
int chs;
cout << "1. Find student by id." << endl;
cout << "2. Exit." << endl;
cin >> chs;
return chs;
}
修复:更新了这两个函数:
Student* splitString(string record)
{
Student* stu = new Student();
stu->id = record.substr(0, 4);
stu->name = record.substr(5, record.length());
return stu;
}
void init(string inputs[], MyHash* myHash, int size)
{
for(int i = 0;i < size; i++)
{
Student* s = new Student();
s = splitString(inputs[i]);//,myHash->students[i]);
//cout << stus[i].name << " " << stus[i].id << endl;
//myHash->insertItemCollision(myHash->students[i].id, myHash->students[i].name);
myHash->insertItemCollision(s->id, s->name);
}
}
问题是您在插入(insertItemCollision
)时正在使用成员students
,但在拆分(splitString
)时也是如此。所以它们被覆盖了。
相关文章:
- 使用对象的基类部分模板专用化对对象进行哈希处理::哈希
- 标准库容器最简单、性能差的哈希类是什么?
- 为自定义类C++实现哈希
- 我可以比朴素哈希表更快地将随机字符串映射到两个类吗?
- 通过自定义结构和链接列表类中的C 中的哈希表
- 错误:"哈希"不是类模板
- 如何为模板类实现std ::哈希
- 内部可哈希类声明失败
- 将密钥哈希存储在类中以提高效率
- 用户定义类的哈希函数.如何交朋友?:).
- 哈希类哈希
- 将_back推入哈希地图类中的向量
- 函数指针(类成员函数)的哈希映射
- 杂音哈希 - 哈希值不一致
- 调用哈希类函数
- 如何在无序映射中为自定义类重用字符串类的哈希函数
- C++ 重写<T> T 派生类的哈希
- 使用哈希表中的派生类搜索函数
- 析构函数,它还为C++中的特定哈希类释放内存
- 从C++到哈斯克尔类和状态