c++自定义结构体做map的key值需要重载<
struct loc {
int x;
int y;
int val;
bool operator<(const loc &l1) const{
if (this->x == l1.x) {
return this->y < l1.y;
} else {
return this->x < l1.x;
}
}
};
map<loc, int> m;
注意重载时函数后要加const, 表示this指针为const指针, 不能在函数内修改其成员变量, 不加可能会报错