#include <iostream>
#include <vector>
#include <string>
#include <math.h>
using namespace std;
struct Node{
string data;
Node* next;
Node(){
data = "";
next = NULL;
}
};
int computeHash(string s, int m){
int p = 1000000007;
int x = 263;
unsigned long long sum = 0;
unsigned long long val = 0;
for(int i = 0; i < s.length(); i++){
val = pow(x, i);
sum = (sum + s[i] * val) % p;
}
sum = sum % m;
return sum;
}
int main(){
int buckets;
cin >> buckets;
int n;
cin >> n;
string tag;
string s;
vector< vector<string> > myStore(n);
for(int i = 0; i < n; i++){
cin >> s;
myStore.at(i).push_back(s);
cin >> tag;
myStore.at(i).push_back(tag);
}
Node** arr= new Node*[buckets];
for(int i = 0; i < n; i++){
if(!myStore[i][0].compare("add")){
s = myStore[i][1];
int hash = computeHash(s,buckets);
cout << hash << endl;
}
}
return 0;
}
我想编写一个程序来实现哈希与链。我试图创建一个节点数组,以便我可以追加如果两个字符串具有相同的散列值。节点阵列:初始化
但我有一个节点数组初始化的问题。我以为数组中的节点将指向NULL。但是当我尝试在gdb中调试时,它显示了一些其他的东西。
有人可以解释我对这种行为发表评论的错误吗?为什么arr 1和arr [2]指向一些内存位置而不是null。我也尝试删除默认的构造函数,但仍然得到相同的结果。任何帮助,将不胜感激。
对Node ** arr有问题,不与矢量。 – Phaneeth