Get 32-bit hash value from boost::hash

后端 未结 3 2166
名媛妹妹
名媛妹妹 2021-02-16 00:46

I am using boost::hash to get hash value for a string. But it is giving different hash values for same string on Windows 32-bit and Debian 64-bit systems.

S

3条回答
  •  伪装坚强ぢ
    2021-02-16 01:17

    What is the guarantee concerning boost::hash? I don't see any guarantees that a generated hash code is usable outside of the process which generates it. (This is frequently the case with hash functions.) If you need a hash value for external data, valid over different programs and different platforms (e.g. for a hashed access to data on disk), then you'll have to write your own. Something like:

    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( *current );
        }
        return results;
    
    }
    

    should do the trick, as long as you don't have to worry about porting to some exotic mainframes (which might not support uint32_t).

提交回复
热议问题