第一个问题:
你正在路过string
作为实例化的第二个模板参数unordered_set<>
类模板。第二个参数应该是哈希函数的类型 http://en.cppreference.com/w/cpp/container/unordered_set, and std::string
不是可调用对象。
也许是想写:
unordered_set<Interval, /* string */ Hash> test;
// ^^^^^^^^^^^^
// Why this?
另外,我建议使用除begin
and end
为您的(成员)变量,因为这些是 C++ 标准库的算法名称。
第二个问题:
你应该记住,哈希函数应该被限定为const http://en.cppreference.com/w/cpp/container/unordered_set/hash_function,所以你的函子应该是:
struct Hash {
size_t operator() (const Interval &interval) const {
// ^^^^^
// Don't forget this!
string temp = to_string(interval.b) +
to_string(interval.e) +
to_string(interval.proteinIndex);
return (temp.length());
}
};
第三个问题:
最后,如果你想要的话std::unordered_set
能够使用类型的对象Interval
,您需要定义一个与您的哈希函数一致的相等运算符。默认情况下,如果不指定任何类型参数作为第三个参数std::unordered_set
类模板,operator ==
将会被使用。
您当前没有任何超载operator ==
为你的班级Interval
,所以你应该提供一个。例如:
inline bool operator == (Interval const& lhs, Interval const& rhs)
{
return (lhs.b == rhs.b) &&
(lhs.e == rhs.e) &&
(lhs.proteinIndex == rhs.proteinIndex);
}
结论:
经过上述所有修改后,您的代码将变为:
#include <string>
#include <unordered_set>
#include <list>
using namespace std;
struct Interval {
unsigned int b;
unsigned int e;
bool updated; //true if concat. initially false
int patternIndex; //pattern index. valid for single pattern
int proteinIndex; //protein index. for retrieving the pattern
};
bool operator == (Interval const& lhs, Interval const& rhs)
{
return (lhs.b == rhs.b) && (lhs.e == rhs.e) && (lhs.proteinIndex == rhs.proteinIndex);
}
struct Hash {
size_t operator()(const Interval &interval) const {
string temp = to_string(interval.b) + to_string(interval.e) + to_string(interval.proteinIndex);
return (temp.length());
}
};
int main()
{
unordered_set<Interval, Hash> test;
list<Interval> concat;
for(list<Interval>::iterator i = concat.begin(); i != concat.end(); ++i){
test.insert(*i);
}
}