【发布时间】:2021-06-26 20:53:36
【问题描述】:
我想实现哈希表,到目前为止,当我想将第一个字符串写入 Tabh 时,我已经从文件中读取并且某些内容不起作用。代码编译没有错误,但控制台中没有任何内容。有人可以帮我吗?我认为我错误地实现了: if (Tabp[ite].substr(0) == "a"){...}
#include <iostream>
#include <fstream>
using namespace std;
void Add(string T[], int r, long k, string w);
//void print();
//void delete();
//void stop();
int main()
{
string linie;
int licznik = 0;
ifstream policz("test.txt");
while(getline(policz, linie))
licznik++;
policz.close();
string * Tabp = new string[licznik];
fstream plik;
plik.open("test.txt");
string wiersz;
for (int b =0; b<licznik; b++)
{
getline(plik, wiersz);
Tabp[b] = wiersz;
}
string iloscprzypadkow = Tabp[0].substr(0);
int n = stoi(iloscprzypadkow);
string rozmiarstring = Tabp[1].substr(5,7);
int size = stoi(rozmiarstring);
string * Tabh = new string[size];
for (int ite=2; ite<licznik; ite++)
if (Tabp[ite].substr(0) == "a")
{
string temp = Tabp[ite].substr(4,5);
long klucz = stoi(temp);
string wartosc = Tabp[ite].substr(7,14);
Add(Tabh, size, klucz, wartosc);
}
//if (wyrazenie == "p")
//{
//}
//if (wyrazenie == "d")
//}
//{
//if (wyrazenie == "s")
//{
//}
}
void Add(string Tabh[], int rozmiar, long klucz, string wartosc)
{
cout<<rozmiar<<klucz<<wartosc;
int indeks = klucz%rozmiar;
Tabh[indeks] = wartosc;
cout<<indeks<<" "<<klucz<<" "<<wartosc;
}
-------------------------
input:
1
size 10
add 13 ala
print
add 23 ola
print
delete 13
print
stop
【问题讨论】:
-
听起来是学习使用调试器的最佳时机。另外,考虑添加一些错误处理:
if (!policz) std::cerr << std::strerror(errno) << "\n";。最后,请格式化您的代码。 -
⟼请记住,尤其是在 Stack Overflow 上学习和提问时,尽可能让代码保持井井有条是很重要的。 Consistent indentation 有助于传达结构,更重要的是,有助于传达意图,这有助于我们快速找到问题的根源,而无需花费大量时间尝试解码正在发生的事情。
-
@rustyx@tadma - 这是我的第一篇文章,所以我
m sorry that I wrong pasted my code. Previousily I checked in debugger and I dont 看看哪一行是错误的;(你能提示我我的代码的哪一部分是错误的,我不能通过一个变量去塔布?