我有以下代码来制作unordered_set<Interval>.编译好了.
struct Interval {
unsigned int begin;
unsigned int end;
bool updated; //true if concat. initially false
int patternIndex; //pattern index. valid for single pattern
int proteinIndex; //protein index. for retrieving the pattern
};
struct Hash {
size_t operator()(const Interval &interval);
};
size_t Hash::operator()(const Interval &interval){
string temp = to_string(interval.begin) + to_string(interval.end) + to_string(interval.proteinIndex);
return hash<string>()(temp);
}
unordered_set<Interval, string, Hash> test;
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用此代码插入时,我无法编译:
for(list<Interval>::iterator i = concat.begin(); i != concat.end(); ++i){
test.insert((*i));
}
Run Code Online (Sandbox Code Playgroud)
而且,我无法确定错误消息的问题,例如:
note: candidate is:
note: size_t …Run Code Online (Sandbox Code Playgroud) 我std::unordered_set第一次使用a 并且对哈希函数有疑问.据我所知,如果你没有指定哈希函数,它将默认为std::hash<Key>.
我的一个mySet班级有一名成员:
typedef std::unordered_set<MyClass> USetType;
USetType mySet;
Run Code Online (Sandbox Code Playgroud)
当我尝试构建时,我收到以下错误:
错误C2440:'type cast':无法从'const MyClass'转换为'size_t'
size_t如果要使用unordered_set自定义类,是否需要定义转换函数(to )?有没有办法避免编写自己的哈希函数并只使用默认值?