c++ - 映射仅匹配结构中包含的一半键
问题描述
我目前正在尝试使用一个结构来包含一个坐标 (x,y),并且遇到了我的地图声明的问题,map<coordinate, bool>
其中坐标是位置,布尔值是它是否被填充。
我的结构:
typedef struct coordinate {
int x;
int y;
friend bool operator>(const coordinate c1, const coordinate c2) {
return c1.x > c2.x;
}
friend bool operator<(const coordinate c1, const coordinate c2) {
return c1.x < c2.x;
}
friend ostream& operator<<(ostream &os, const coordinate c1) {
os << "(" << c1.x << "," << c1.y << ")";
return os;
}
friend bool operator==(const coordinate c1, const coordinate c2) {
return c1.x == c2.x && c1.y == c2.y;
}
} coordinate;
以及我使用的方法map::find
:
bool Board::Insert(int playerID, int x, int y){
if(x > M || y > M){
outOfBoundsError();
return false;
}
else if(playerMap.find(playerID) != playerMap.end()){
cout << "Player with ID " << playerID << " already exists\n";
return false;
}
else {
coordinate pos;
pos.x = x;
pos.y = y;
if(gameMap.find(pos) == gameMap.end()){
Player newPlayer = Player(playerID, pos);
gameMap[pos] = true;
playerMap[playerID] = pos;
N++;
return true;
}
else {
cout << "Player already exists at position " << pos << "\n";
}
return false;
}
}
我不确定我是否遗漏了一些简单的东西,例如需要完成的运算符重载,但我找不到map::find
函数中如何比较这些值。