1

我正在使用 anunordered_map<float, unsigned short>在 C++ 中实现哈希表。

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

但是,我想知道如何unordered_map散列我的浮点数以估计碰撞频率的详细信息。创建unordered_map. 根据文档,默认哈希函数是std::hash<Key>. 在我的情况下是std::hash<float>。但是,当我查看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

4

1 回答 1

1

根据 C++11 标准,float也支持std::hash。实际的散列函数取决于实现,因此即使您可以计算出当前编译器的冲突频率,较新版本或不同的编译器也可能实现不同的散列函数。以下是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*>;
于 2011-10-13T18:08:21.770 回答