使用我创建的自定义类作为键在地图中搜索键时,我遇到了奇怪的行为。
尽管它们存在于地图中,但它似乎没有找到密钥。
有谁知道这是什么原因?
代码(可以在这里运行):
#include <iostream>
#include <map>
using namespace std;
typedef short int dimension;
class Point {
public:
Point() : m_x(0), m_y(0) {}
Point(const Point &other) : m_x(other.m_x), m_y(other.m_y) {};
Point(dimension x, dimension y) : m_x(x), m_y(y) {}
bool operator<(const Point &other) const {
if (m_x < other.m_x) return true;
return (m_y < other.m_y);
}
private:
dimension m_x;
dimension m_y;
};
int main() {
map<Point, bool> points = {{Point(0, 2), true},
{Point(1, 1), true},
{Point(2, 4), true}}; …Run Code Online (Sandbox Code Playgroud)