Get 32-bit hash value from boost::hash

后端 未结 3 1801
感动是毒
感动是毒 2021-02-16 00:29

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

提交回复
热议问题