unorder_map<浮动 ,短>为什么这有效?浮动>
我正在使用 unordered_map
在 C++ 中实现哈希表。
我知道在大多数情况下,使用浮点数作为哈希表的键是一个坏主意,因为比较它们很容易出错。然而,在这些情况下,我从大文件中读取浮点数,并且它们的精度是已知的且恒定的。
但是,我想知道 unordered_map
如何对我的浮点数进行哈希处理以估计碰撞频率的详细信息。创建 unordered_map
时,我不会覆盖默认的哈希实现。根据文档,默认的哈希函数是 std::hash
。在我的例子中是 std::hash
。但是,当我查看 std::hash
文档时,它仅为“char*
类型的模板参数,const char*
”定义、 crope
、wrope
和内置整数类型”。
有谁知道当我将值添加到 unordered_map 时调用什么函数来哈希值?
unordered_map
- http://msdn.microsoft.com /en-us/library/bb982522.aspx
std::hash
- http://www.sgi.com/tech/stl/hash.html#1< /a>
I am using an unordered_map<float, unsigned short>
to implement a hash table in C++.
I know that using floats as keys to a hash table is a bad idea under most circumstances because comparing them is error-prone. However, under these circumstances, I am reading the floats in from large files and their precision is known and constant.
However, I would like to know the details of how unordered_map
is hashing my floats in order to estimate collision frequency. I am not overriding the default hash implementation when I create the unordered_map
. According to the documentation, the default hash function is std::hash<Key>
. Which in my case is std::hash<float>
. However, when I look at the std::hash
documentation, it is only defined for "template arguments of type char*
, const char*
, crope
, wrope
, and the built-in integral types".
Does anyone know what function is being called to hash the values as I add them to the unordered_map?
unordered_map
- http://msdn.microsoft.com/en-us/library/bb982522.aspx
std::hash
- http://www.sgi.com/tech/stl/hash.html#1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 C++11 标准,
std::hash
也支持float
。实际的哈希函数取决于实现,因此即使您可以计算出当前编译器的冲突频率,较新的版本或不同的编译器也可能实现不同的哈希函数。以下是std::hash
专业化的完整列表:According to the C++11 standard,
float
is supported forstd::hash
as well. The actual hash function is implementation dependent, so even if you can figure out collision frequency for your current compiler a newer version or a different compiler may implement a different hash function. Here is the full list ofstd::hash
specializations: