C++ でハッシュテーブルを使用するプログラムを作成しようとしています。基本的な考え方は、多くのデータ ポイントがあり、新しいポイントが与えられたときに、それが既に存在するかどうかを知ることができるようにハッシュ テーブルを使用したいということです。しかし、そこにはいくつかのバグがあり、それを修正する方法は本当にありません。(エラーメッセージ: 'bool Point::operator==(const Point&)' の 'this' 引数として 'const Point' を渡すと修飾子が破棄されます) よろしくお願いします。
#include <iostream>
#include <unordered_map>
using namespace std;
class Point {
public:
Point(int _x, int _y):x(_x), y(_y) {}
bool operator==(const Point& lhs)
{ return this->x==lhs.x && this->y ==lhs.y; }
private:
int x;
int y;
};
int main ()
{
Point p1=Point(1,2);
Point p2=Point(2,3);
Point p3=Point(4,5);
unordered_map<Point,bool> mymap = {{p1,true},{p2,true},{p3,true} };
Point p4=Point(1,2);
unordered_map<Point,bool>::const_iterator got = mymap.find(p4);
if (got == mymap.end())
cout << "not found";
else
cout << "already exists";
cout<<endl;
return 0;
}