我正在尝试在Node.js中实现C++的std::hash功能,并对它们进行比较。由于std::hash不基于任何加密算法,我无法理解它是如何在内部生成的。
C++中的用法参考:https://iq.opengenus.org/std-hash-cpp/
发布于 2021-11-09 08:47:46
10秒钟的搜索结果是following fragment for libcxx (Clang附带的C++ STL ):
// We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
// is 64 bits. This is because cityhash64 uses 64bit x 64bit
// multiplication, which can be very slow on 32-bit systems.
template <class _Size, size_t = sizeof(_Size)*__CHAR_BIT__>
struct __murmur2_or_cityhash;
template <class _Size>
struct __murmur2_or_cityhash<_Size, 32>
{
inline _Size operator()(const void* __key, _Size __len)
_LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK;
};libstdc++ seems to use murmur hash也是,尽管哈希函数是许多简单类型的标识函数(例如char,int,double,...)
给未来读者的注意:在写这篇文章的时候,这个答案是正确的。hash背后的算法将来可能会改变!
https://stackoverflow.com/questions/69894021
复制相似问题