std::hash实现太简单分布不匀

std::hash实现太简单分布不匀

(金庆的专栏 2017.5)

#include <iostream>
#include <functional>

using namespace std;

int main()
{
    std::hash<int> hasher;
    cout << hasher(2) << endl;
    cout << hasher(3) << endl;
    cout << hasher(4) << endl;

    return 0;
}


输出为
jinqing@server:~/test$ g++ main.cpp -std=c++11
jinqing@server:~/test$ ./a.out
2
3
4

查看实现,/usr/include/c++/5/bits/functional_hash.h

      operator()(_Tp __val) const noexcept              \
      { return static_cast<size_t>(__val); }            \

所以对分布有要求的,应该使用自己的hash, 不要使用 std::hash.

boost::hash 的实现也是简单取值,
boost_1_60_0/boost/functional/hash/hash.hpp

    template <typename T>
    typename boost::hash_detail::basic_numbers<T>::type hash_value(T v)
    {
        return static_cast<std::size_t>(v);
    }

Boost说明了hash用于STL容器,而不是其它。
    This hash function is designed to be used in containers based on the STL and is not suitable as a general purpose hash function.
    
VS2015会使用 FNV-1a

    size_t operator()(const argument_type& _Keyval) const
        {    // hash _Keyval to size_t value by pseudorandomizing transform
        return (_Hash_seq((const unsigned char *)_Keyval.c_str(),
            _Keyval.size() * sizeof (_Elem)));
        }


    inline size_t _Hash_seq(const unsigned char *_First, size_t _Count)
        {    // FNV-1a hash function for bytes in [_First, _First + _Count)
            ...


但 FNV-1a 也不是通用的 hash 函数,如果输入值相近,则其输出值也相近。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值