用于哈希的哈希集函数
Hash set function for hashing
我试图实现一个哈希集,但我有一些麻烦的哈希函数。我想在集合中添加姓名和电话号码:
class Person{
string name;
long long int phoneNumber;
}
集合中的索引是通过对phoneNumber的数字求和来计算的。问题是我不希望我的函数是这样的:
int add(long long int nr, Element e) - the function that adds an Element to the set
{
int hashCode = hash(nr);;
...
}
,其中long long int nr
应为phoneNumber, Element e
应为Person。我是说,这很愚蠢。如果我已经有person作为参数,为什么还要有phoneNumber呢?正如您所看到的,我正在使用模板,我的老师建议我为hashFunction做一个虚拟类,以强制它成为各自的类型(类似于Java中的HashSet)。问题是我不知道该怎么做。你有什么可以帮助我的主意吗?
如果你这样做了:
int add(Person p, Element e)
你将限制你的hasset类完全依赖于Person
类。以这种方式将整数或字符串值放入哈希方法中更合理。您可以在散列方法之外确定要散列的参数,并将其作为输入。
此外,当您向Person类添加另一个成员(如age)并在散列中使用in时,您还需要修改散列方法以使用该特定成员
相关文章:
- 如何为位集找到/实现一个好的哈希函数
- 对于短字符串来说,这是一个很好的哈希函数吗?
- 我在C++中使用提升哈希函数将 3 个双精度组合成一个面临冲突的哈希
- 无冲突的哈希函数
- 在 C++ 中为特定哈希表创建插入函数
- Qt5 哈希函数在 Windows 7 和 Windows10 上不会返回相同的哈希
- 在C++中是否已经为文件系统迷你过滤器驱动程序编写了一个计算MD5哈希的函数
- 哈希集、哈希映射和哈希表
- 自定义无序集哈希函数
- hash_set哈希器函数
- 调用哈希类函数
- 用于哈希的哈希集函数
- c++如何将数组插入哈希集
- 哈希集搜索函数不工作c++
- 用于生成密码哈希的C/ c++函数(使用MD5或其他算法)
- unordered_map自定义哈希/相等函数 - 函数不会被调用
- (C++) 无法迭代密钥哈希器函数中的向量
- 如何使用模板类的模板类子类从哈希表实现哈希集
- 如何将C++ "int"添加到哈希集<Integer>?
- 为什么这个哈希表函数显示编译错误