使用对数据类型的向量的哈希表中的分段错误错误
Segmentation fault error in Hash table using vectors of pair data type
我正在尝试使用对数据类型的向量实现哈希表。我认为我已经实现了哈希表的原理工作,没有任何编译错误,但是当我尝试运行时,它会抛出分段错误错误。
包括
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
template < class T >
class HTable {
private:
vector < pair < string, T >> data;
vector < bool > positions_in_use;
size_t d_size;
public:
HTable(size_t size): d_size(size) {
data.resize(d_size);
positions_in_use.resize(d_size);
};
//hashing function to convert strings into indexes
int hash(string key) {
int hash = 0;
int index;
for (size_t i = 0; i < key.length(); i++) {
hash = hash + (int) key[i];
}
index = hash % d_size;
return index;
};
//Function to insert pairs into hash table using hash function
bool insert(const string & key,
const T & value) {
int index;
pair < string, T > emptypair;
index = hash(key);
int pos = index;
if (data.at(index) != emptypair) {
typename vector < pair < string, T >> ::iterator it;
data.insert(it + index, pair < string, T > (key, value));
positions_in_use[index] = true;
cout << "linear";
return true;
} else {
bool foundplace = false;
while (!foundplace) {
pos++;
if (data.at(index) == emptypair) {
typename vector < pair < string, T >> ::iterator it;
data.insert(it + pos, pair < string, T > (key,value));
positions_in_use[pos] = true;
foundplace = true;
return foundplace;
} else if (pos == index) throw runtime_error("Hash Table is full!");
if (pos == d_size - 1)
pos = 0;
}
return false;
}
}
//Function to get the value of key that is stored into hash table
T & get(const string & key) {
int index = hash(key);
int pos = index;
if (data.at(index).first == key) {
return data.at(index).second;
} else {
bool foundkey = false;
while (!foundkey) {
pos++;
if (data.at(pos).first == key) {
foundkey = true;
return data.at(pos).second;
} else if (pos == index) throw runtime_error("Not contained in Hash Table!");
if (pos == d_size - 1)
pos = 0;
}
}
}
//erase vector pair at certain index in hash table
void erase(const string & key) {
pair < string, T > emptypair;
int pos = hash(key);
int index = pos;
if (data.at(index).first == key) {
data.at(index) = emptypair;
positions_in_use[index] = 0;
} else {
index++;
if (index = d_size)
index = 0;
else if (index = pos) throw runtime_error("Not contained in Hash Table!");
}
}
//clear entire hash table
void clear() {
data.clear();
positions_in_use.clear();
}
//display all the hash table contents
friend ostream & operator << (ostream & os,
const HTable & h) {
for (size_t i = 0; i < h.data.size(); i++) {
os << h.data[i].first << "," << h.data[i].second << endl;
}
return os;
}
};
int main() {
try {
HTable < int > hs(10);
int score = 43;
string name = "peter";
hs.insert(name, score);
cout << hs;
} catch (const exception & e) {
std::cout << e.what();
}
}
超时:受监视的命令转储核心sh:第 1 行:141719分段故障超时 10s
data.insert(it + index, ...)
通过访问未初始化的变量it
表现出未定义的行为
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)