【发布时间】:2021-08-24 21:37:46
【问题描述】:
我很难调用 hash_value。
从这个post,我想申请vector<vector<E>>,其中E 是一个自定义对象。
我的代码如下:
struct E;
class myclass {
private:
vector<E> lhs;
public:
myclass(const vector<E>& v) :lhs{ v } { };
static size_t hash_value(const vector<E>& v) {
size_t seed = 0;
boost::hash_combine(seed, d.name);
boost::hash_combine(seed, d.scope);
return seed;
}
bool operator==(const vector<E> >& rhs) {
for (unsigned i = 0; i < lhs.size(); i++)
if (lhs[i].name != rhs[i].name || lhs[i].scope!= rhs[i].scope)
return false;
return true;
};
};
然后我调用这个代码:
void test(std::vector<std::vector<E>>& A)
{
boost::unordered_set < myclass > input_records(A.size());
for (BOOST_AUTO(it, A.begin()); it != (A.end());) {
auto k = input_records.insert(myclass{*it}); <<--
....
}
}
但是我得到一个错误:
此外,在某些情况下,此代码会执行,但从未调用 hash_value。 我不确定我错过了什么?
我该如何解决这个问题?
【问题讨论】: