我正在寻找有关C++ std::hash<std::string>
特化中使用哪种哈希算法的信息。我能得到的最接近的信息是我的include/c++/7/bits/basic_string.h
的信息:
/// std::hash specialization for string.
template<>
struct hash<string>
: public __hash_base<size_t, string>
{
size_t
operator()(const string& __s) const noexcept
{ return std::_Hash_impl::hash(__s.data(), __s.length()); }
};
然后
include/c++/7/bits/functional_hash.h
: struct _Hash_impl
{
static size_t
hash(const void* __ptr, size_t __clength,
size_t __seed = static_cast<size_t>(0xc70f6907UL))
{ return _Hash_bytes(__ptr, __clength, __seed); }
template<typename _Tp>
static size_t
hash(const _Tp& __val)
{ return hash(&__val, sizeof(__val)); }
template<typename _Tp>
static size_t
__hash_combine(const _Tp& __val, size_t __hash)
{ return hash(&__val, sizeof(__val), __hash); }
};
最后
include/c++/7/bits/hash_bytes.h
: // Hash function implementation for the nontrivial specialization.
// All of them are based on a primitive that hashes a pointer to a
// byte array. The actual hash algorithm is not guaranteed to stay
// the same from release to release -- it may be updated or tuned to
// improve hash quality or speed.
size_t
_Hash_bytes(const void* __ptr, size_t __len, size_t __seed);
实际上有两个问题:
_Hash_bytes
的算法是什么? 最佳答案
保留hash function的详细信息作为实现详细信息。对于两个相等的值,散列需要在程序执行期间返回相同的值,并且返回的哈希值应均匀地分布在返回值的范围内。 (不需要散列在不同的执行中返回相同的值,可以使用加盐的散列。)
由于Hash_bytes
函数是特定于实现的函数(名称开头和下划线,后跟一个大写字母,是实现保留的标识符),因此您必须在实现的库源代码中查找该函数以了解其功能。
关于c++ - C++ std中使用的哈希算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60904661/