C++ 为什么在执行main之前我就遇到了分段错误(内核转储)?
我有一个赋值,在这里我必须使用完全散列来散列输入数组中的名称,并给出名称的数量 这是我的代码: 字典C++ 为什么在执行main之前我就遇到了分段错误(内核转储)?,c++,segmentation-fault,global-variables,C++,Segmentation Fault,Global Variables,我有一个赋值,在这里我必须使用完全散列来散列输入数组中的名称,并给出名称的数量 这是我的代码: 字典 #include <string> #include <vector> using namespace std; class Dictionary { public: // Insert an input set of n keys to the dictionary. This method should print out the following inf
#include <string>
#include <vector>
using namespace std;
class Dictionary {
public:
// Insert an input set of n keys to the dictionary. This method should print out the following information:
// 1. The hash functions comprising the perfect hash (both levels)
// 2. The sum of squares of the number of keys mapped to each bin of the first level hash function, and
// 3. The number of trials needed to generate each hash function.
void bulkInsert(int n, string *keys);
// Insert a key to the dictionary.
// Print out whether the insertion caused a collision in the second level hash.
// Handle collision with separate chaining.
void insert(string key);
// Remove a key from the dictionary, if it exists.
void remove(string key);
// Return whether a key is found in the dictionary.
// Print the buckets (both first and second level) accessed during the operation.
bool find(string key);
//make random matrix and print after
void makeRandom(int nR, int nC);
//format key and hash for first level, return index of hash
int firstLevelHash(string input, int nR, int nC);
//print hash
void printHash();
private:
vector<vector<int>> randomMat;
vector<vector<int>> randomMat2;
vector<vector<string>> hashTable;
};
#包括
#包括
使用名称空间std;
类词典{
公众:
//向字典中插入一组n个键的输入集。此方法应打印出以下信息:
//1.包含完美哈希的哈希函数(两个级别)
//2.映射到第一级哈希函数的每个bin的键数的平方和,以及
//3.生成每个哈希函数所需的试验次数。
插入(整数n,字符串*键);
//插入字典的键。
//打印出插入是否导致第二级哈希冲突。
//使用单独的链接处理碰撞。
无效插入(字符串键);
//从字典中删除密钥(如果存在)。
无效删除(字符串键);
//返回是否在字典中找到键。
//打印操作期间访问的存储桶(第一级和第二级)。
布尔查找(字符串键);
//制作随机矩阵,打印后打印
void makeRandom(整数编号,整数编号);
//格式化第一级的键和散列,返回散列的索引
int firstLevelHash(字符串输入,int nR,int nC);
//打印散列
void printHash();
私人:
向量随机矩阵;
向量随机矩阵2;
向量哈希表;
};
Dictionary.cpp
#include "Dictionary.h"
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <algorithm>
using namespace std;
void Dictionary::bulkInsert(int n, string *keys) {
int numRow = (ceil(log2(n)));
int numCol = 64;
int trialNum = 1;
bool repeat = true;
while ( repeat ) {
//make random matrix
makeRandom(numRow, numCol);
//check hashtable empty, clear if not
if ( !( hashTable.empty() ) ) {
hashTable.clear();
}
//create hashtable, given by 2^numRow
for ( int i = 0; i < ( 2^numRow ); i++ ) {
vector<string> row;
hashTable.push_back(row);
}
//loop through keys and hash first level
for ( int i = 0; i<n; i++) {
int ind = firstLevelHash(keys[i], numRow, numCol);
hashTable[ind].push_back(keys[i]);
}
//check condition on whether to repeat hash
int limit = 4*n;
int summation = 0;
for ( int i = 0; i < hashTable.size(); i++ ) {
summation = summation + ((hashTable[i].size())^2);
}
if ( summation < limit ) {
repeat = false;
}
//second level hashing
}
}
void Dictionary::insert(string key) {
}
void Dictionary::remove(string key) {
}
bool Dictionary::find(string key) {
return false;
}
//randomizes randomMat and prints;
void Dictionary::makeRandom(int nR, int nC) {
vector<int> row;
for ( int i = 0; i < nR; i++ ) {
row.clear();
for ( int j = 0; j < nC; j++ ) {
int random = rand()%2;
row.push_back(random);
}
randomMat.push_back(row);
}
}
//returns vector for key of length 64
int Dictionary::firstLevelHash(string input, int nR, int nC) {
//check length of input
//if less than 8, add space to front
if ( input.size() < 8 ) {
for( int p = 0; p < 8-(input.size()); p++) {
input = " " + input;
}
}
//obtain string of length 64 of bits of input
string binStr = "";
for ( char& chr : input) {
binStr += bitset<8>(chr).to_string();
}
vector<int> keyVector;
//go through string and convert each to bit
for ( int i = 0; i < binStr.size(); i++ ) {
string substr = binStr.substr(i, 1);
int val = stoi(substr);
keyVector.push_back( val );
}
vector<int> hashFinalValue;
//multiply by hash
for ( int j = 0; j < nR; j++ ) {
int hashVal = 0;
for ( int p = 0; p < nC; p++ ) {
hashVal = hashVal + (randomMat[j][p] * keyVector[p]);
}
hashVal = hashVal % 2;
hashFinalValue.push_back( hashVal );
}
//convert binary to decimal to get index
int index = 0;
int q = 1;
for ( int m = 0; m < hashFinalValue.size(); m++ ) {
index += hashFinalValue[m] * ( 2^(hashFinalValue.size()-q) );
q++;
}
return index;
}
//prints hashTable
void Dictionary::printHash() {
for ( int i = 0; i < hashTable.size(); i++ ) {
for ( int j = 0; j < hashTable[i].size(); j++ ) {
cout << hashTable[i][j];
cout << " | ";
}
cout << "\n";
}
}
#include "Dictionary.h"
#include <iostream>
using namespace std;
int main(){
cout<<"adfad";
Dictionary dict;
string strs[] = {"Fred Astaire", "Lauren Bacall", "Brigitte Bardot", "John Belushi", "Ingmar Bergman"};
int n = 5;
dict.bulkInsert(n, strs);
dict.printHash();
return 0;
}
#包括“Dictionary.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
void Dictionary::bulkInsert(int n,string*键){
int numRow=(ceil(log2(n));
int numCol=64;
int-trialNum=1;
布尔重复=真;
while(重复){
//生成随机矩阵
makeRandom(numRow,numCol);
//检查哈希表是否为空,如果不是,则清除
if(!(hashTable.empty()){
hashTable.clear();
}
//创建哈希表,由2^numRow给出
对于(int i=0;i<(2^numRow);i++){
向量行;
hashTable.push_back(行);
}
//循环遍历键并在第一级散列
对于(int i=0;i您关于程序在到达main
之前失败的断言是错误的。事实上,它在此处崩溃:
#0 0x00007ffff7f60484 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x0000555555558783 in __gnu_cxx::new_allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::construct<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&> (this=0x5555555742b0, __p=0x31,
__args#0="Lauren Bacall") at /usr/include/c++/8/ext/new_allocator.h:136
#2 0x00005555555579ce in std::allocator_traits<std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::construct<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&> (__a=..., __p=0x31,
__args#0="Lauren Bacall") at /usr/include/c++/8/bits/alloc_traits.h:475
#3 0x00005555555570ea in std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::push_back (this=0x5555555742b0, __x="Lauren Bacall") at /usr/include/c++/8/bits/stl_vector.h:1079
#4 0x000055555555650b in Dictionary::bulkInsert (this=0x7fffffffdb40, n=5, keys=0x7fffffffdaa0) at Dictionary.cc:39
#5 0x000055555555af9c in main () at my_test_dictionary.cc:12
只执行一次,而不是您预期的8次。有关解释,请参阅。您是否调试了?或者您是否认为segfault出现在main之前只是因为“adfad”以前没有输出吗?如果是后者,请调试。还要添加一个换行符或刷新以强制该行中的输出,而不是更晚。执行调试生成,并且不进行优化OT:使用命名空间std;
在源文件中表示一种非常糟糕的做法,在头文件中表示一种邪恶的做法。std::cout
的打印输出会被缓冲和删除如果发生segfault(通常由UB引起),则不需要打印。2^numRow
很可能不是您的意思。((哈希表[i].size())^2
//create hashtable, given by 2^numRow
for ( int i = 0; i < ( 2^numRow ); i++ ) {