谢谢。 我尝试过:Hi,I'm trying to hash english words from a txt file and store them in a table using chaining method . but i don't know what is wrong when i try to search and compare .this Error :"Exception thrown: read access violation.search was 0x1.If there is a handler for this exception, the program may be safely continued."thanks.What I have tried:#include <iostream>#include <string>#include <sstream>#include <fstream>using namespace std;#define a 1#define b 2#define p 311#define size 355063struct Node{char* word;Node* next;};int ASCII_sum(char* word){int sum=0;for (int i = 0;i<strlen(word);i++) {="" sum="" +="word[i];" }="" return="" sum-2;="" int="" h(int="" k)="" (((a*k="" b)="" %="" p)="" size);="" void="" insert(node**="" d,="" index,="" node*="" word)="" if="" (d[index]="=" null)="" d[index]="word;" d[index]-="">next = NULL;}else{word->next = D[index];D[index] = word;}}Node** Hash(char* file_name){FILE* dictionary = fopen(file_name, "r");char w[128];Node** Dictionary = new Node* [size];for (int j = 0;j < size;j++) { Dictionary[j] = NULL; }int i = 0;while (i<354986){fscanf(dictionary, "%s",w);Node* new_word = new Node;//new_word->word = strcpy(new_word->word, w);new_word->word = w;//insertion:int index = H(ASCII_sum(w));if (Dictionary[index] == NULL){new_word->next = NULL;Dictionary[index] = new_word;}else{new_word->next = Dictionary[index];Dictionary[index] = new_word;}//insertion//insert(Dictionary, H(ASCII_sum(w)), new_word);//cout << Dictionary[index]->word<< endl;i++;}return &Dictionary[size];}/*void print_index(Node** table, int index){Node* P = table[index];while (P != NULL){cout << P->word<<endl;P = P->next;}}*/bool check_sentence(string s,Node** Dectionary){bool found = true;istringstream split(s);while (split){string tmp;split >> tmp;if (tmp == "") continue;char *t = new char[tmp.length()+1];t = strcpy(t, tmp.c_str());Node* search = Dectionary[H(ASCII_sum(t))];cout << H(ASCII_sum(t))<<endl;if (search == NULL){return false;}while (search != NULL){if (strcmp(search->word, t)){cout << search->word;found = true;break;}else{search = search->next;cout << search->word << endl;}}return false;delete [] t;}return found;}void case_insensitive(char* text){return;}int main() {cout << "Processing...\n";Node* table = *Hash("words.txt");cout << "Finished.\n";string s;cin >> s;if (check_sentence(s,&table)){cout << "True\n";}else{cout << "False\n";}}推荐答案假设它不是作业,你应该使用std::unordered_map [ ^ ]。Assuming it is not homework, you should use std::unordered_map[^].引用:抛出异常:读取访问冲突。 搜索为0x1。 如果有此异常的处理程序,程序可以安全地继续。"Exception thrown: read access violation.search was 0x1.If there is a handler for this exception, the program may be safely continued."没有办法简单处理异常就可以让你的程序安全地继续。 这条消息告诉你在'指针初始化','内存分配'或'指针方面有问题搞乱。问题可能是上述问题的组合。 调试器是跟踪此类问题的最佳工具。 您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。 调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ] 调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 /> 调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。 建议:拿一张纸,试着手工完成,你的程序应该使用相同的程序。 Nota:CP中的错误改变了你的代码。There is no way that a simple handling of the exception can allow your program to continue safely.This message kindly tell you that you have a problem in 'pointer initializing', 'memory allocation' or 'pointer messing'. The problem can be a combination of the above.The debugger is the best tool to track this kind of problem.You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.Debugger - Wikipedia, the free encyclopedia[^]Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]The debugger is here to show you what your code is doing and your task is to compare with what it should do.There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.Advice: take a sheet of paper and try to do it by hand, your program should use the same procedure.Nota: a bug in CP have altered your code.fscanf(dictionary, "%s",w);Node* new_word = new Node;//new_word->word = strcpy(new_word->word, w);new_word->word = w; 因此,字典中的每个Node.word都指向属于哈希函数,一旦该函数返回就会消失。你需要为每个单词分配内存并从w复制字符串,如:So every Node.word in your dictionary points to a temporary block of memory that belongs to the Hash function and will disappear as soon as that function returns. You need to allocate the memory for each word and copy the string from w, like:Node* new_word = new Node;int size = strlen(w) + 1; // size of required buffernew_word->word = new char[size];strcpy(new_word->word, w); 这篇关于我有散列问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 16:31
查看更多