C++ C+中带有列表类的迭代器Segfault+;
我正在为我的一个comp-sci类做一个项目,该类使用哈希表将字符串从STL的List类排序为链表向量。将字符串插入列表时,除索引为3外的所有索引都会导致SEGFAULT错误。我不知道为什么会这样。下面是插入函数的代码以及我收到的一些错误示例。向量“table”被声明为在默认构造函数中包含4个元素C++ C+中带有列表类的迭代器Segfault+;,c++,string,list,hashtable,C++,String,List,Hashtable,我正在为我的一个comp-sci类做一个项目,该类使用哈希表将字符串从STL的List类排序为链表向量。将字符串插入列表时,除索引为3外的所有索引都会导致SEGFAULT错误。我不知道为什么会这样。下面是插入函数的代码以及我收到的一些错误示例。向量“table”被声明为在默认构造函数中包含4个元素 void Stringset::insert(string word) { cout << "insertion" << endl; hash<strin
void Stringset::insert(string word)
{
cout << "insertion" << endl;
hash<string> stringHash;
int hashIndex = stringHash(word) % size;
cout << hashIndex << endl;
bool exists = false;
//find intended index and create base boolean variable for whether or not the value already exists
list<string>::iterator pos = table[hashIndex].begin();
list<string>::iterator end = table[hashIndex].end();
for(pos; pos != end; pos++){
cout << "pass" << endl;
if((*pos).compare(word) == 0){
exists = true;
}
}
if(!exists){
table[hashIndex].push_back(word);
num_elems++;
cout << "inserted " << (*pos) << endl;
}
else{
}
}
连同一个单独的案例:
I: insert word
F: find word
R: remove word
P: print words in stringset
Q: quit
I
Enter word to insert: Error
insertion
2
Segmentation fault (core dumped)
Stringset.h以及Stringset对象默认构造函数:
#pragma once
#include <string>
#include <vector>
#include <list>
using namespace std;
//Stringset class, do not modify definitions for existing members
class Stringset
{
private:
vector<list<string>> table;
int num_elems;
int size;
public:
Stringset();
vector<list<string>> getTable() const;
int getNumElems() const;
int getSize() const;
void insert(string word);
bool find(string word) const;
void remove(string word);
};
Stringset::Stringset() : table(4), num_elems(0), size(4) {}
#pragma一次
#包括
#包括
#包括
使用名称空间std;
//Stringset类,不修改现有成员的定义
类字符串集
{
私人:
向量表;
整数元素;
整数大小;
公众:
Stringset();
向量getTable()常量;
int getNumElems()常量;
int getSize()常量;
空白插入(字符串字);
布尔查找(字符串字)常量;
无效删除(字符串字);
};
Stringset::Stringset():表(4),元素数(0),大小(4){}
我相当肯定,一旦程序进入for循环,它总是会崩溃,但我不知道为什么,因为我不熟悉迭代器。任何关于如何解决这个问题的想法都将不胜感激。我会仔细检查hashIndex计算。这可能会导致SEG故障。此外,您可以使用一些C++语法来为您头痛。看看下面的变化:
//list<string>::iterator pos = table[hashIndex].begin();
//list<string>::iterator end = table[hashIndex].end();
bool exists = false;
for (auto& Pos : table[hashIndex]) {
cout << "pass" << endl;
if (Pos.compare(word) == 0) {
exists = true;
break;
}
}
if (!exists) {
table[hashIndex].push_back(word);
num_elems++;
//cout << "inserted " << (*pos) << endl;
//Pos was pointing to the .end() in your coude
cout << "inserted " << table[hashIndex].back() << endl;
}
//list::iterator pos=表[hashIndex].begin();
//列表::迭代器end=表[hashIndex].end();
bool exists=false;
用于(自动位置:表[hashIndex]){
你的代码基本上是正确的
错误在您的计算机中,表如何定义和填充,以及与表相关的大小设置为多少?为什么不使用表。大小()
相反?你能展示你的构造函数吗?更具体地说,你能展示表
是如何初始化的吗?你能为表向量的所有索引创建一个列表对象吗?你的例子表明只有最后一个条目被正确初始化,而其他条目要么被单位化,要么为空。我怀疑你的错误在你的构造函数中UCTCHONTENT: <代码> CUT @约翰“一旦找到匹配的单词,你就可以从搜索循环中得到代码<破译<代码> >,或者更好地考虑使用<代码> STD::FunthIf()/DoC>代替手工循环:<代码(FixIIF)(表[哈希索引] .开始),表[HAsHealth],Enter(),[&](Strand s){StraseString(Word)=0;} = =表[HaseCurk.Enter()){…}
@Remy停止编辑你的评论,这样我就不必继续投票了。
//list<string>::iterator pos = table[hashIndex].begin();
//list<string>::iterator end = table[hashIndex].end();
bool exists = false;
for (auto& Pos : table[hashIndex]) {
cout << "pass" << endl;
if (Pos.compare(word) == 0) {
exists = true;
break;
}
}
if (!exists) {
table[hashIndex].push_back(word);
num_elems++;
//cout << "inserted " << (*pos) << endl;
//Pos was pointing to the .end() in your coude
cout << "inserted " << table[hashIndex].back() << endl;
}