2011-10-13 40 views
1

我正在使用unordered_map> float,unsigned short>来实现C++中的哈希表。unorder_map <float,short>为什么这有效?

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

但是,我想知道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 ::散列 - http://www.sgi.com/tech/stl/hash.html#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*>; 
+0

感谢,应该知道他们在C++ 11 .. – JHowIX

+0

@JHowIX更新哈希:它之前没有C++ 11 _exist_。 –