I use Node to hold (x, y) of coordinator and try to insert 4 nodes into map. The following code only print 2 nodes, why?
{0,0}, 0
{1,2}, 3
If i change the code of overload operator '<' with
bool operator<(const Node &ob) const
{
return x < ob.x or y < ob.y;
}
It prints all 4 nodes. My understanding the operator< only impact the ordering of keys.
code
#include <iostream>
#include <map>
template<typename T1, typename T2>
struct Node
{
T1 x;
T2 y;
Node(T1 x, T2 y): x(x), y(y) {}
bool operator<(const Node &ob) const
{
return x < ob.x and y < ob.y;
}
bool operator==(const Node &ob) const
{
return x == ob.x and y == ob.y;
}
};
int main()
{
std::map<Node<int, int>, int> map =
{
{{0, 0}, 0},
{{0, 1}, 1},
{{1, 0}, 2},
{{1, 2}, 3}
};
for (const auto &entry: map)
{
auto key = entry.first;
std::cout << "{" << key.x << "," << key.y << "}, " << entry.second << std::endl;
}
return 0;
}