suf*_*que 21 c++ string hash hashcode
以下java代码返回字符串的哈希码.
String uri = "Some URI"
public int hashCode() {
return uri.hashCode();
}
Run Code Online (Sandbox Code Playgroud)
我想将此代码翻译为c ++.c ++中是否有任何可用的功能或一种简单的方法来翻译它.
Cat*_*lus 63
在C++ 03中boost::hash.在C++ 11中std::hash.
std::hash<std::string>()("foo");
Run Code Online (Sandbox Code Playgroud)
tun*_*2fs 15
Boost提供了一个哈希函数:
#include <boost/functional/hash.hpp>
int hashCode()
{
boost::hash<std::string> string_hash;
return string_hash("Hash me");
}
Run Code Online (Sandbox Code Playgroud)
小智 6
以下是String.hashCode()Java中默认的源代码,这是一个在C++中实现的trival练习.
public int hashCode()
{
int h = hash;
if (h == 0 && count > 0)
{
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++)
{
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
Run Code Online (Sandbox Code Playgroud)