我正在尝试创建一个包含指向同一类的指针的std::unordered_set
的类,但是在声明该类之前无法找到准备哈希函数的方法。
struct hash
{
inline std::size_t operator()(Vertex const * & other);
};
struct Vertex
{
// ...
double x, y, z;
std::unordered_set<Vertex *, hash> touching;
};
inline std::size_t hash::operator()(Vertex * const & other) const
{
return ((hash<double>()(other->x))>>1)^
((hash<double>()(other->y)) )^
((hash<double>()(other->z))<<1);
}
最佳答案
我假设您的意思是std::hash
中的 hash::operator()
(如果这样),请指定完整范围并包括 <functional>
。然后所有人都需要Vertex
类的前向声明,然后是all fine
#include <unordered_set>
#include <functional> // std::hash
struct Vertex; // forward declare the `Vertex`
struct hash
{
std::size_t operator()(Vertex* other) const;
};
struct Vertex
{
double x, y, z;
std::unordered_set<Vertex*, ::hash> touching;
};
std::size_t hash::operator()(Vertex* other) const
{
return ((std::hash<double>()(other->x)) >> 1) ^
((std::hash<double>()(other->y))) ^
((std::hash<double>()(other->z)) << 1);
}
另外,请注意,您不需要采用指针的
const
-ref(即Vertex const*& other
),而只需采用pass it by value for primitive types即可。
关于c++ - 类具有std::unordered_set的相同类的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62738521/