[英]C++ printing array
I'm working on linear and quadratic probing hash table implementation in C++. 我正在研究C ++中的线性和二次探测哈希表实现。 In Hash.cpp, I have a working linearProb(int key) and quadProb functions. 在Hash.cpp中,我有一个有效的linearProb(int键)和quadProb函数。 If I call them separately through main.hpp, it prints out correct hash table, but I want to see the result of both linear and quadratic tables when I compile. 如果我通过main.hpp分别调用它们,它将打印出正确的哈希表,但是我想在编译时看到线性表和二次表的结果。
This is my linearProb (quadProb looks similar) 这是我的linearProb(quadProb看起来很相似)
void Hash::linearProb(int key){
int i, count = 0;
Hash h;
//if it is empty, place it there
if (a[key % tableSize] == -1)
a[key % tableSize] = key;
else{
i = 0;
//until finding an empty slot, but don't loop around
while (i < tableSize && a[i] != -1){
count++;
i++;
}
if(count == tableSize){
cout<<key<<" could not be inserted in the table\n";
exit(1);
}
//when there's a collision increase i by 1 until finding empty slot
for(i = (key % tableSize+1) % tableSize; i <tableSize; i++){
if(a[i] == -1){
a[i] = key;
break;
}
}
}
}
and I also have print() in Hash.cpp 我在Hash.cpp中也有print()
void Hash::print(){
int i;
//cout<<"Hash Table with Linear Probing"<<endl;
cout<<"\n Result Hash Table: "<<endl;
for(i = 0; i < tableSize; i++){
cout<<"\n"<<i;
if(a[i] != -1){
cout<<" "<< a[i];
}
}
cout<<"\n";
}
If I call it in main.cpp like this 如果我这样在main.cpp中调用它
int main(){
int key;
Hash h;
//take in .txt file
std::fstream file;
file.open("keys.txt");
while(!file.eof()){
file >> key;
if(key != -1){
h.linearProb(key);
//h.quadProb(key);
}
}
file.close();
if(key == -1){
h.print();
}
}
I can see that my probing works, but notice that I commented out quadProb in order to test linearProb. 我可以看到我的探测有效,但是请注意,为了测试linearProb,我注释掉了quadProb。 I want to print out both tables at the same time. 我想同时打印两个表。 In order to do that, I attempted to call print() in each probing function instead of calling it from main. 为了做到这一点,我尝试在每个探测函数中调用print(),而不是从main调用它。
This is what I tried. 这就是我尝试过的。 I changed main() to 我将main()更改为
while(!file.eof()){
file >> key;
h.linearProb(key);
//h.quadProb(key);
}
file.close();
and added to linearProb(int key) 并添加到linearProb(int键)
void Hash::linearProb(int key){
int i, count = 0;
Hash h;
if(key == -1){
h.print();
exit(1);
}
}
But this only print out 0~9 without a[i]. 但这仅打印出0〜9而没有a [i]。 When I tested what a[i] is when it enters print(), and it gives me all i value has a[i] of -1, which leads to not printing out anything. 当我测试a [i]进入print()时是什么时,它给我的所有i值的a [i]均为-1,这导致不打印任何内容。 I'm really confused why this is happening. 我真的很困惑为什么会这样。 Why is print() not getting correct a[i] even though it worked when I called print() through main? 为什么当我通过main调用print()时,print()无法获得正确的a [i]?
In your "print from probe function", you print an empty hash h declared in the function. 在“从探测函数打印”中,打印在函数中声明的空哈希h。 You should drop that Hash h;
您应该删除该Hash h;
and just call print()
instead of h.print()
. 然后只需调用print()
而不是h.print()
。
This is a nice problem that a debugger can help you with. 这是一个很好的问题,调试器可以帮助您。 When breaking in that branch, it would show an empty h
, while in main, h
would be filled. 在该分支中中断时,它将显示一个空的h
,而在主分支中, h
将被填充。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.