问题:在哈希表中打印项目时,非顶级项目无法正确打印。 我从文本文件中逐行添加项目。首先,确定行数,然后构造哈希表,并在文件的第一个循环中找到其大小。
hash::hash(unsigned int tSize)
{
this->tableSize = tSize;
hashTable.resize(tableSize);
for (int i = 0; i < tableSize; i++)
{
hashTable[i] = new item;
hashTable[i]->firstLetters = "__";
hashTable[i]->str = "____";
hashTable[i]->next = NULL;
}
}
void hash::add(std::string key)
{
int index = hafn(key);
if (hashTable[index]->str == "____")
{
hashTable[index]->str = key;
setFirstLetters(index,key[0],key[1]);
}
else
{
item *iter = hashTable[index];
item *n_ptr = new item;
n_ptr->str = key;
setFirstLetters(n_ptr, key[0], key[1]);
n_ptr->next = NULL;
while (iter->next != NULL)
{
iter = iter->next;
}
iter->next = n_ptr;
}
}
void hash::setFirstLetters(item *n_ptr, char a, char b)
{
n_ptr->firstLetters[0] = a;
n_ptr->firstLetters[1] = b;
}
void hash::setFirstLetters(int index, char a, char b)
{
hashTable[index]->firstLetters[0] = a;
hashTable[index]->firstLetters[1] = b;
}
void hash::print()
{
int num;
for (int i = 0; i < tableSize; i++)
{
num = numInIndex(i);
printer(num, i);
if (num > 1)
{
int c = 0;
for (int j = num - 1; j > 0; j--)
{
printer(c, num, i);
c++;
}
}
}
}
void hash::printer(int num, int i)
{
item *iter = hashTable[i];
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << iter->firstLetters << endl;
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}
void hash::printer(int numIn, int num, int i)
{
item *iter = hashTable[i];
for (int j = 0; j < numIn; j++)
{
iter = iter->next;
}
cout << "-------------------------------" << endl;
cout << "index = " << i << endl;
cout << iter->str << endl;
cout << std::flush;
cout << iter->firstLetters << endl; //this does not work, though the pointer is pointing to the correct values
//printf("%s\n", iter->firstLetters.c_str()); //this works, even without flushing
cout << "# of items = " << num << endl;
cout << "-------------------------------" << endl;
cout << endl;
}
struct item
{
std::string str;
std::string firstLetters;
item *next;
};
问题是firstLetters
无法正确打印。 firstLetters
设置正确。但是,在第三级和更高级别的项目(即,使用相同的哈希索引)中,firstLetters
根本不会打印。
为了更清楚,这是一个输出示例:
-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------
-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------
-------------------------------
index = 15
good
# of items = 3
-------------------------------
注意,在“添加项目,设置值并打印它们”标题下,在方法hash::add(std::string key)
中,我使用setFirstLetters()
访问std::string firstLetters
中的元素,而无需首先进行初始化他们。这实际上导致这些值的任何更改都丢失。当需要打印值时访问iter->firstLetters
时,无法访问任何实际数据。为了避免这种不确定的行为,我尝试更改hash::add(std::string key)
的定义以设置firstLetters
的值。
hash::add()
void hash::add(std::string key)
{
int index = hafn(key);
if (hashTable[index]->str == "____")
{
hashTable[index]->str = key;
setFirstLetters(index,key[0],key[1]);
}
else
{
item *iter = hashTable[index];
item *n_ptr = new item;
n_ptr->firstLetters = "__"; //here
n_ptr->str = key;
setFirstLetters(n_ptr, key[0], key[1]);
n_ptr->next = NULL;
while (iter->next != NULL)
{
iter = iter->next;
}
iter->next = n_ptr;
}
}
添加此行可修复输出。
这是新的输出:
-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------
-------------------------------
index = 15
will
wi
# of items = 3
-------------------------------
-------------------------------
index = 15
good
go
# of items = 3
-------------------------------
这种类型的行为算作“未定义的行为”。这里有几个移动的部分,这种现象的唯一原因是由于缺少初始化。如果std::cout
失败但printf()
失败,请确保在尝试访问它们之前先初始化所有成员/变量。在使用std::string
的特定情况下,[]
运算符只能在正常初始化后使用=
运算符或std::string
的其他成员函数才能正常运行。