Reputation: 77
struct vector_nodes_less
{
inline bool operator()(const Vector2i& a, const Vector2i& b) const
{
if (a.x == b.x && a.y == b.y) return false;
if (a.x < b.x && a.y < b.y) return true;
if (a.x > b.x && a.y > b.y) return false;
return (a.x < b.x || a.y < b.y);
}
};
typedef std::map <Vector2i, node*, vector_nodes_less> vector_nodes;
When I do operations with the above type it throws invalid operator<.
It happens when the keys (0,0) (0,1) and I perform an operation on a (1,0) vector key.
I understand I need to have weak strict ordering but my function should be OK shouldnt it?
Any help would be most welcome as this is slowing me down.
Regards
Sam
Upvotes: 1
Views: 423
Reputation: 47619
It's not allowed to have both a < b
and b < a
So if you operator returns true for pair of elements it should return false for them in inversed orders. Now consider pairs (0, 1) and (1, 0)
The easiest way to write operator (if you don't care about specific order) is to reuse pair's operator<
return std::tie(a.x, a.y) < std::tie(b.x, b.y);
Upvotes: 7