这一次我能够显示完整的代码:C++ unordered_map问题
#include <unordered_map>
#include <iostream>
#include <stdlib.h>
using namespace std;
bool mystrcmp(const char *s1, const char *s2) {
int i = 0;
do {
if(s1[i] != s2[i])
return false;
} while(s1[i++] != '\0');
return true;
}
struct eqstr
{
bool operator()(const char* s1, const char* s2) const
{
return mystrcmp(s1, s2);
}
};
int main(void) {
char buffer[5] = {'h', 'e', 'd', 'e', '\0'};
unordered_map<char *, int , hash<char *> , eqstr> int_from_symbols;
int_from_symbols["hede"] = 1;
int_from_symbols["hodo"] = 2;
unordered_map<char *, int , hash<char *> , eqstr>::const_iterator it = int_from_symbols.find(buffer);
eqstr myeq;
if(myeq("hede",buffer))
fprintf(stderr, "no problem here\n");
if(it == int_from_symbols.end())
fprintf(stderr, "dammit\n");
else fprintf(stderr, "%d\n", int_from_symbols[buffer]);
return 0;
}
此输出:
no problem here
dammit
任何想法是怎么回事?
在此先感谢,,
厄尼尔
我不认为全球专门研究'hash'这是一个好主意 - 除此之外,这可能会让打破ODR变得非常容易。为什么不像往常一样定义一个自定义函数,并将其作为模板参数传递给'unordered_map'? –
2009-12-25 07:38:14
请检查您的标题,了解散列专业化。 –
2009-12-25 10:34:08
这真的很烦人。我实现了其他散列函数,将结构编码为字符串,并使用散列来散列这些散列函数。那些哈希函数正常工作,如果哈希工作不正确,为什么其他工作正常? –
2009-12-25 17:31:34