unorder_map< float,short>为什么这个工作? [英] unorder_map<float, short> Why does this work?

查看:168
本文介绍了unorder_map< float,short>为什么这个工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个unordered_map> float,unsigned short>在C ++中实现一个哈希表。



我知道使用float作为哈希表的键是坏的想法在大多数情况下,因为比较它们是容易出错的。然而,在这种情况下,我正在阅读大文件中的浮点数,并且它们的精度已知并且是不变的。然而,我想知道unordered_map的细节对我的浮动进行散列以估计碰撞频率。当我创建无序映射时,我不重写默认哈希实现。根据文档,默认的散列函数是std :: hash> Key>。在我的情况下是std :: hash> float>。但是,当我查看std :: hash文档时,它仅被定义为仅为类型为char *,const char *,crope,wrope和内置整型类型的模板参数定义。



有没有人知道函数被调用来散列值,因为我将它们添加到unordered_map?



unordered_map - http://msdn.microsoft.com/zh-cn/library/bb982522.aspx < a>



std :: hash - http://www.sgi.com/tech/stl/hash.html#1

解决方案

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

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


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 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

解决方案

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*>;

这篇关于unorder_map&lt; float,short&gt;为什么这个工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆