나는 완전한 코드를 보여줄 수있어이 시간 :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
무슨 일이 일어나고 있는지 어떤 생각을? 사전에
감사합니다 ,,
하기 Onur
전 세계적으로 'hash'을 전문적으로 다루는 것은 좋은 생각이 아닙니다. 다른 것들 중에서도 ODR을 깨뜨리기가 쉽습니다. 왜 그냥 커스텀 펑터를 정의하고 그것을 평소와 같이'unordered_map'의 템플릿 인수로 전달하는 것이 좋을까요? –
이것은 정말로 성가신 일입니다. 구조를 문자열로 인코딩하고 해시를 사용하여 해시를 해시하는 다른 해시 함수를 구현했습니다. 이러한 해시 함수가 제대로 작동했는데 해시 이 제대로 작동하지 않으면 다른 것들이 올바르게 작동 한 이유는 무엇입니까? –