哈希表C 字符串
hash table c++ strings
试图在C 中实现哈希表,该表必须在该表中获取字符串数据,并且必须至少保存10个项目。在下面实现了这一点,但没有编译并以某种方式打破了它:(,开放有关如何最好地实施或解决此问题的其他想法
谢谢请有人成为传奇。:)
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class hash{
private:
static const int tableSize = 10;
struct item
{
string d;
item* next;
};
item* HashTable[tableSize];
public:
hash();//the constructor
int Hash(string key);
void AddItem(string d);//will add new item
int NumberOfItemsInBucket(int bucket);
void PrintTable();
void PrintItemsInBucket(int bucket);
};
hash::hash()
{
for(int i = 0;i < tableSize;i++)
{
HashTable[i] = new item;
HashTable[i]->d = "";
HashTable[i]->next = NULL;
}
};
void hash::AddItem(string d)
{
int bucket = Hash(d);
if(HashTable[bucket]->d == "")
{
HashTable[bucket]->d = d;
}
else
{
item* Ptr = HashTable[bucket];
item* n = new item;
n->d = d;
n->next = NULL;
while(Ptr->next != NULL)
{
Ptr = Ptr->next;
}
Ptr->next;
}
}
int hash::NumberOfItemsInBucket(int bucket)
{
int slot = 0;
if(HashTable[bucket]->d == "")
{
return slot;
}
else
{
slot++;
item* Ptr = HashTable[bucket];
while(Ptr->next != NULL)
{
slot++;
Ptr = Ptr->next;
}
}
return slot;
}
void hash::PrintTable()
{
int number;
for(int i = 0;i < tableSize;i++)
{
number = NumberOfItemsInBucket(i);
cout << "--------------------n";
cout << "bucket = " << i << endl;
cout << "Data: " << HashTable[i]->d << endl;
cout << "No. of items = " << number << endl;
cout << "--------------------n";
}
}
void hash::PrintItemsInBucket(int bucket){
item* Ptr = HashTable[bucket];
if(Ptr->d == ""){
cout << "bucket " << bucket << " is empty!n";
}else{
cout << "Bucket " << bucket << " contains this: " << endl;
while(Ptr != NULL){
cout << "--------------------n";
cout << Ptr->d << endl;
cout << "--------------------n";
Ptr = Ptr->next;
}
}
}
int hash::Hash(string key){
int hash = 0;
int index;
for(int i = 0;i < key.length();i++)
{
hash = hash + (int)key[i];
//cout << "Hash = " << hash << endl; //displays the hash function result
}
index = hash % tableSize;
return index;
}
int main (){
hash newHash;
newHash.AddItem("restaurant");
newHash.AddItem("innovation");
newHash.AddItem("vegetarian");
newHash.AddItem("opposition");
newHash.AddItem("attractive");
newHash.AddItem("incredible");
newHash.AddItem("assessment");
newHash.AddItem("illustrate");
newHash.AddItem("presidency");
newHash.AddItem("background");
newHash.PrintTable();
//newHash.PrintItemsInBucket();
return 0;
}
编译错误:注意:班级哈希错误:范围中未声明的" newhash"错误:引用"哈希"是模棱两可的
只需删除使用Manespace STD即可删除;并显式添加添加std ::到endl,cout和string。
相关文章:
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 用C++将哈希表写入文件并从文件中恢复
- C++中的并发哈希表
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 与C++哈希表的基础知识混淆
- 调整大小和复制哈希表数组中的元素
- 我可以比朴素哈希表更快地将随机字符串映射到两个类吗?
- 自定义哈希表实现-将字符串映射到整数时出现内存错误
- 在哈希表中调整字符串数组的大小
- 哈希表C 字符串
- 哈希表,其中键是字符串,值是 c++ 中的函数
- 实现将字符串映射到 C++ 中的数组的哈希表。我一直在"Debug Assertion Failed",我不知道为什么
- 使用向量<对<字符串,int > >表示哈希表
- 在C++中创建用于字符串操作的哈希表
- 用于构建高效哈希表的字符串的唯一属性
- 如何散列字符串?我需要把随机字符串转换成整数,放到哈希表中
- c++中字符串的哈希表
- 在哈希表中创建字符串哈希值的时间复杂度
- c++哈希表,其中键是字符串,值是字符串的向量