返回向量的单个索引值
Returning individual index values of a vector
我的任务是散列向量并返回向量中每个元素的散列索引值。但是,我的代码为每个元素返回相同的值,但我觉得我只需要移动一些东西来解决问题。到目前为止,我拥有的:
void HashNames::hash(string fileName)
{
ifstream infile(fileName);
string tmp;
for (int i = 0; i < 1002; i++)
{
infile >> tmp;
this->nameList.push_back(tmp);
for (int i = 0; i < nameList.size(); i++)
{
this->index = djbHash(tmp, 53);
}
}
}
int HashNames::djbHash(string data, int table)
{
int hashVal = 5381;
for (int i = 0; i < data.length(); i++)
{
hashVal *= 33;
hashVal += static_cast<int>(data[i]);
}
hashVal %= table;
return abs(hashVal);
}
void HashNames::printNames()
{
for (int i = 0; i < this->nameList.size(); i++)
{
cout << this->index << ": " << this->nameList[i] << endl;
}
}
以及该类的外观:
class HashNames
{
private:
int index;
vector<string> nameList;
int djbHash(string data, int table);
public:
void hash(string fileName);
void printNames();
};
我想我自己想通了,我只是做了另一个整数向量并做到了:
for (int i = 0; i < nameList.size(); i++)
{
this->index = djbHash(tmp, 53);
this->hashTable.push_back(index);
}
相关文章:
- 如何在for循环中包含两个索引值的测试条件
- *无符号字符中的索引值
- 返回向量的单个索引值
- 如何在对向量排序后更改索引值?c++
- 为什么C++数组索引值是有符号的,而不是围绕size_t类型构建的(或者我错了)
- 如何计算 3d 数组中的索引值在内存中的位置?如何计算字符**中的索引值在内存中的位置?
- 如何在C++中为数组分配不同的索引值
- C++枚举索引值
- 查找索引值在char表中
- 根据索引值提取特定字节
- 尝试输出所有索引值(如果找到)
- 有没有办法创建一个花哨的迭代器和相应的新数组,以便检查每个索引的索引值的条件?
- 为什么在将单词从文本文件C++到行进行多重映射时获得额外的索引值
- C++使用 min_element 或 max_element 时检索向量中的索引值
- 如何使用二进制索引树来计算小于索引值的元素数量
- 存储数组索引值C++
- 在 for 循环中使用 int 以外的数据类型作为索引值C++
- 向函数传递特定的数组索引值
- C++从迭代器中获取整数形式的索引值
- 将数组中的所有数字更改为其索引值