I have a simple Position struct:
struct Position
{
int x;
int y;
};
I also have a list of Positions:
std::list<Position> positons;
I’m trying to sort the list using list::sort() and need to define operator< for Positions objects. I tried keeping it simple creating something like:
bool operator<(const Position& one, const Position& two)
{
return one.x < two.x && one.y < two.y;
}
But that doesn’t work. How do I determine that one class/struct object as a whole is less than another? How would I do it for my Position struct?
EDIT When I call positions.sort(), I get a debug assertion failed which says:
Expression: invalid operator<
You can sort by x and then by y. Also define it as free function:
Or as
operator: