std::hash
在思考使用const char*
来作为std::unordered_map
的时候,遇到了hash的问题,然而在cppreference std::hash 中有这样的描述:
There is no specialization for C strings. std::hash
produces a hash of the value of the pointer (the memory address), it does not examine the contents of any character array.
因此,我只能怪customize hash for const char*
。
std::hash
in C++ standard library的概述
在 stackoverflow std::hash value on char* value and not on memory address? 的回答中的一段话较好地概述了std::hash
in C++ standard library:
Sadly enough, the current C++ standard library doesn't provide general purpose hash algorithms disentangled(拜托) from object-specific hash solutions. (But there is some hope this could change in the future.)
显然,这段话告诉了我们: 目前C++ standard library的std::hash
是object-specific hash function,并没有提供general purpose hash algorithm。
Change in the future: open-std Types Don't Know
NOTE: 在 stackoverflow std::hash value on char* value and not on memory address? 的回答中"But there is some hope this could change in the future" some hope 所链接的是 open-std Types Don't Know 提案,本文就是基于这篇文章:
This paper proposes a new hashing infrastructure that completely decouples hashing algorithms from individual types that need to be hashed. This decoupling divides the hashing computation among 3 different programmers who need not coordinate with each other.