我正在研究一个应该采用mxn字符数组的程序,基本上是一个单词搜索,并找到这个拼图中的所有单词(向前,向上,向下,向后和对角线)在字典里。我的代码中的所有内容都有效,直到我尝试将字典存储到哈希表中(我需要为分配执行此操作)。到目前为止,这是我的代码以及相关的.h和.cpp文件:
hash_table.h:
#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
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;
}
}
}
最后,main.cpp:
#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;
}
一旦代码到达最终的for循环就会出现问题,然后程序崩溃。 &#34; main.exe已停止工作,windows正在寻找解决方案....&#34;