从boost :: hash获取32位哈希值

use*_*350 6 c++ hash boost

boost::hash用来获取字符串的哈希值.但它为Windows 32位和Debian 64位系统上的相同字符串提供了不同的哈希值.

那么boost::hash无论平台如何,我如何获得相同的哈希值(32位或64位)?

Jam*_*nze 5

什么是保证boost::hash?我没有看到任何保证生成的哈希码在生成它的过程之外可用。(散列函数经常出现这种情况。)如果您需要外部数据的散列值,在不同的程序和不同的平台上有效(例如,对磁盘上的数据进行散列访问),那么您必须编写自己的。就像是:

uint32_t
hash( std::string const& key )
{
    uint32_t results = 12345;
    for ( auto current = key.begin(); current != key.end(); ++ current ) {
        results = 127 * results + static_cast<unsigned char>( *current );
    }
    return results;

}
Run Code Online (Sandbox Code Playgroud)

应该可以解决问题,只要您不必担心移植到某些异国主机(可能不支持 uint32_t)。