We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
template <> struct hash<string> { std::size_t operator()(string const &val) const noexcept { return std::hash<char const *>{}(val.c_str()); } };
这里边自定义的hash对于string为key时用的是字符串的地址值作为散列函数的输入,正常来说不是应该按照string的内容来散列的吗?这么设计是有什么考虑吗?
The text was updated successfully, but these errors were encountered:
有道理,这里确实有问题,误把std::hash<char const *>作为C字符串hash函数了。这里之所以不直接用std::hash<std::string>的原因是内存构造器不一样,会导致字符串产生一次深拷贝;另外为了兼容C++14,std::string_view的特化也不方便直接用。
std::hash<char const *>
std::hash<std::string>
std::string_view
看来这里需要自己手写一个字符串哈希函数。
https://en.cppreference.com/w/cpp/utility/hash https://stackoverflow.com/questions/34597260/stdhash-value-on-char-value-and-not-on-memory-address https://stackoverflow.com/questions/7666509/hash-function-for-string
Sorry, something went wrong.
mutouyun
No branches or pull requests
这里边自定义的hash对于string为key时用的是字符串的地址值作为散列函数的输入,正常来说不是应该按照string的内容来散列的吗?这么设计是有什么考虑吗?
The text was updated successfully, but these errors were encountered: