C++ 检查等值线c++;使用无序集
i>使用C++ > unordeDeSt/<代码>检查等值词。< /P>C++ 检查等值线c++;使用无序集,c++,c++11,C++,C++11,i>使用C++ > unordeDeSt/检查等值词。< /P> struct CustomHasher { size_t operator()(const char& c) const; }; // This hashing function should take the given character c and return an integer // representing the hash value. This will be computed by the p
struct CustomHasher {
size_t operator()(const char& c) const;
};
// This hashing function should take the given character c and return an integer
// representing the hash value. This will be computed by the position of a-z,
// where a=>0, b=>1, and so on.
size_t CustomHasher::operator()(const char& c) const {
size_t i = tolower(c) - 'a';
return i;
}
void add_multiset(const string& s,
unordered_multiset<char, CustomHasher>* ms) {
for (int i = 0; i < s.length(); i++)
ms->insert(tolower(s[i]));
}
// inside main function
unordered_multiset<char, CustomHasher> ms;
add_multiset("hello", &ms);
struct CustomHasher{
size_t运算符()(const char&c)const;
};
//这个散列函数应该接受给定的字符c并返回一个整数
//表示哈希值。这将由a-z的位置计算,
//其中a=>0,b=>1,依此类推。
size\u t CustomHasher::operator()(const char&c)const{
尺寸i=低于(c)-‘a’;
返回i;
}
void add_multiset(常量字符串和字符串),
无序_多集*ms){
对于(int i=0;i插入(tolower(s[i]);
}
//内部主功能
无序多集ms;
添加_multiset(“hello”、&ms);
我的代码有什么问题?当我检查:ms.bucket('l')
的输出时,我应该得到11,但是我得到了7
同样ms.bucket('o')
我得到6,但我应该得到14
我的代码有什么问题?桶号不一定等于散列。对于一个几乎为空的
无序的\u multiset
,您会期望许多散列共享同一个存储桶
由于
unordered\u multiset
是一个模板类,因此您可以很容易地跟踪代码,以了解它是如何基于散列计算桶数的。我知道有很多方法,但是我想用我自己的方法知道问题出在哪里?你的问题是你假设散列码和桶号应该是相同的。它们不是。你能解释一下我如何修复运算符()代码吗?我放了注释,但对于我的代码和bucjet,它应该使用我的函数返回字母表是位置thanks@salim.p这就是我想告诉你的,你的代码工作正常。你的期望是错误的。