我想做的是使 std::unordered_set 与我的自定义类 Vector2 一起使用成为可能 -包括搜索已经在 set 中的此类的对象的可能性。
让我提供更多细节。我的 Vector2 类的标头(包括自定义哈希表)如下:
Class Vector2
{
private:
static int lastID;
const int id;
int x;
int y;
public:
Vector2(int _x, int _y);
~Vector2();
bool Vector2::operator==(const Vector2& other) const;
int getId() const;
int getX() const;
int getY() const;
};
namespace std
{
template<>
struct hash<Vector2>
{
size_t
operator()(const Vector2& obj) const
{
return hash<int>()(obj.getId());
}
};
}
Run Code Online (Sandbox Code Playgroud)
此类的成员函数的实现很简单:
int Vector2::lastID = 0;
Vector2::Vector2(int _x, int _y) : id(lastID++)
{
x = …Run Code Online (Sandbox Code Playgroud)