C++ 将字典文本文件加载到哈希表中的问题
我正在开发一个程序,该程序应该采用一个m x n字符数组,基本上是一个单词搜索,并找到这个拼图中字典中的所有单词(向前、向上、向下、向后和对角)。在我尝试将字典存储到哈希表中之前,代码中的所有内容都是有效的(作业要求我这样做)。以下是我迄今为止的代码以及相关的.h和.cpp文件: hash_table.h:C++ 将字典文本文件加载到哈希表中的问题,c++,class,dictionary,hashtable,C++,Class,Dictionary,Hashtable,我正在开发一个程序,该程序应该采用一个m x n字符数组,基本上是一个单词搜索,并找到这个拼图中字典中的所有单词(向前、向上、向下、向后和对角)。在我尝试将字典存储到哈希表中之前,代码中的所有内容都是有效的(作业要求我这样做)。以下是我迄今为止的代码以及相关的.h和.cpp文件: hash_table.h: #ifndef HASH_TABLE_H #define HASH_TABLE_H #include <vector> #include <iostream> #i
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
#include <vector>
#include <iostream>
#include <list>
#include <string.h>
// WANT: support a collection of n items and
// fast implementation of the following operations
// FIND(x): returns true iff x is in collection
// INSERT(x): x is added to collection
// DELETE(x): x is removed from collection
class hash_table
{
public:
// CONSTANTS AND TYPES
static const int DEFAULT_TABLE_SIZE = 20;
typedef std::string value_type;
// CONSTRUCTORS
hash_table(int table_size = DEFAULT_TABLE_SIZE);
// CONSTANT MEMBERS
// pre:
// post: returns yes iff key is in this hash table
bool find(const value_type & key) const;
// pre:
// post: key has been added to this hash table
// if it was not this hash table
// else do nothing
void insert(const value_type &key);
// pre:
// post: key has been deleted from this hash table
// if it was in this table; otherwise do nothing
void erase(const value_type & key);
private:
std::vector<std::list<value_type> > table;
// pre:
// post: returns a table index corresponding to key
std::size_t hash(const value_type & key) const;
};
#endif // HASH_TABLE_H
\ifndef哈希表\u H
#定义哈希表
#包括
#包括
#包括
#包括
//想要:支持n个项目和
//快速实现以下操作
//FIND(x):当x在集合中时返回真值
//插入(x):x被添加到集合中
//删除(x):从集合中删除x
类哈希表
{
公众:
//常数和类型
静态常量int默认表格大小=20;
typedef std::字符串值\u类型;
//建设者
哈希表(整数表大小=默认表大小);
//固定成员
//前:
//post:如果密钥在此哈希表中,则返回yes
布尔查找(常量值、类型和键)常量;
//前:
//post:已将密钥添加到此哈希表
//如果不是这个哈希表
//否则什么也不做
无效插入(常量值类型和键);
//前:
//post:已从此哈希表中删除密钥
//如果它在这张桌子上,否则什么也不做
无效擦除(常量值类型和键);
私人:
std::向量表;
//前:
//post:返回与键对应的表索引
std::大小\u t散列(常量值\u类型和键)常量;
};
#endif//哈希表
hash_table.cpp:
#include "hash_table.h"
std::size_t hash_table::hash(const value_type & key) const
{
std::size_t h = 0;
for (int i = 0; i < key.length(); ++i)
{
h = 5*h + key[i];
}
return h;
}
hash_table::hash_table(int table_size)
{
table = std::vector<std::list<value_type> >(table_size, std::list<value_type>());
}
bool hash_table::find(const value_type &key) const
{
std::list<value_type> l = table[hash(key)];
std::list<value_type>::const_iterator i;
for (i = l.begin(); i != l.end(); ++i)
{
if (*i == key)
{
std::cout << hash(key);
return true;
}
}
return false;
}
void hash_table::insert(const value_type & key)
{
if (find(key))
{
return;
}
table[hash(key)].push_front(key);
}
void hash_table::erase(const value_type & key)
{
std::list<value_type> & l = table[hash(key)]; // note that l is a reference variable
std::list<value_type>::iterator i;
for (i = l.begin(); i != l.end(); ++i)
{
if (*i == key)
{
l.erase(i);
return;
}
}
}
#包括“hash_table.h”
标准::大小\u t哈希表::哈希(常量值\u类型和键)常量
{
标准:尺寸h=0;
对于(int i=0;i>n;
你不能把所有的代码都扔掉,然后说“它不工作”。详细描述你的问题是什么,它发生在什么地方,等等。table[hash(key)]。push_front(key);
-你在哪里设置table[hash(key)]
?这看起来像是一个数组越界错误。使用vector::at()
而不是[]
使用vector的内置边界检查。
#include <iostream>
#include <vector>
#include <fstream>
#include <string.h>
#include <algorithm>
#include "hash_table.h"
using namespace std;
int load(string dict[])
{
ifstream ds("dictionary.txt");
if (ds.fail())
{
cout << "File I/O error!" << endl;
exit(2);
}
string word;
int size(0);
while (ds >> word)
dict[size++] = word;
sort(dict, dict+size);
return size;
}
int main()
{
int m(0), n(0);
cout << "Enter dimensions of array: ";
cin >> m >> n;
cout << endl;
ifstream puzzle("puzzle.txt");
if (puzzle.fail())
{
cout << "Error!" << endl;
exit(2);
}
char letters[m][n];
while (puzzle.good())
{
for (int i = 0; i < m; ++i)
{
for (int j = 0; j <= i; ++j)
{
letters[i][j] = puzzle.get();
}
}
}
cout << "YOUR WORD SEARCH PUZZLE: " << endl << endl;
for (int i = 0; i < m; ++i)
{
for (int j = 0; j <= i; ++j)
{
cout << letters[i][j];
}
}
string dict[100000];
int size = load(dict);
hash_table dic(size);
for (int c = 0; c < size; ++c)
{
dic.insert(dict[c]);
}
return 0;
}