unorder_map<浮动 ,短>为什么这有效?

发布于 2024-12-10 00:45:52 字数 948 浏览 0 评论 0原文

我正在使用 unordered_map 在 C++ 中实现哈希表。

我知道在大多数情况下,使用浮点数作为哈希表的键是一个主意,因为比较它们很容易出错。然而,在这些情况下,我从大文件中读取浮点数,并且它们的精度是已知的且恒定的。

但是,我想知道 unordered_map 如何对我的浮点数进行哈希处理以估计碰撞频率的详细信息。创建 unordered_map 时,我不会覆盖默认的哈希实现。根据文档,默认的哈希函数是 std::hash。在我的例子中是 std::hash。但是,当我查看 std::hash 文档时,它仅为“char* 类型的模板参数,const char*”定义、 cropewrope 和内置整数类型”。

有谁知道当我将值添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾忌 2024-12-17 00:45:52

根据 C++11 标准,std::hash 也支持 float。实际的哈希函数取决于实现,因此即使您可以计算出当前编译器的冲突频率,较新的版本或不同的编译器也可能实现不同的哈希函数。以下是 std::hash 专业化的完整列表:

template <> struct hash<bool>;
template <> struct hash<char>;
template <> struct hash<signed char>;
template <> struct hash<unsigned char>;
template <> struct hash<char16_t>;
template <> struct hash<char32_t>;
template <> struct hash<wchar_t>;
template <> struct hash<short>;
template <> struct hash<unsigned short>;
template <> struct hash<int>;
template <> struct hash<unsigned int>;
template <> struct hash<long>;
template <> struct hash<unsigned long>;
template <> struct hash<long long>;
template <> struct hash<unsigned long long>;
template <> struct hash<float>;
template <> struct hash<double>;
template <> struct hash<long double>;
template <class T> struct hash<T*>;

According to the C++11 standard, float is supported for std::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 of std::hash specializations:

template <> struct hash<bool>;
template <> struct hash<char>;
template <> struct hash<signed char>;
template <> struct hash<unsigned char>;
template <> struct hash<char16_t>;
template <> struct hash<char32_t>;
template <> struct hash<wchar_t>;
template <> struct hash<short>;
template <> struct hash<unsigned short>;
template <> struct hash<int>;
template <> struct hash<unsigned int>;
template <> struct hash<long>;
template <> struct hash<unsigned long>;
template <> struct hash<long long>;
template <> struct hash<unsigned long long>;
template <> struct hash<float>;
template <> struct hash<double>;
template <> struct hash<long double>;
template <class T> struct hash<T*>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文