我对C++非常陌生,我需要将用C编写的一些代码转换为C++。问题是,我发现C++语法在运行中很难理解。我希望使用无序的boost哈希映射,我希望定义我自己的哈希函数。我抬起头怎么做,我整个这一段代码跑:为boost哈希映射定义自定义哈希函数
struct ihash
: std::unary_function<std::string, std::size_t>
{
std::size_t operator()(std::string const& x) const
{
std::size_t seed = 0;
std::locale locale;
for(std::string::const_iterator it = x.begin();
it != x.end(); ++it)
{
boost::hash_combine(seed, std::toupper(*it, locale));
}
return seed;
}
};
Which you can then use in a case insensitive dictionary:
boost::unordered_map<std::string, int, ihash, iequal_to> idictionary;
我有以下的散列函数:
unsigned long hash (unsigned char *str)
{
unsigned long hash = 5381;
int c;
while(c = *str++)
hash = ((hash << 5) + hash) + c; /* hash + 33 + c */
hash %= outer_relation->hash_table.bucket_count();
return hash;
}
有人能帮我转换。特别是种子和结合体负责什么?
,因为它不会是不区分大小写(尽管名称),并且在C++哈希表实现中没有'outer_relation'。对于初学者。 (遗漏了使用哈希表的实现细节来实现通用哈希的糟糕设计的详细信息,IMO) – sehe