If I have a class that I want to be able to sort (ie support a less-than concept), and it has several data items such that I need to do lexicographic ordering then I need something like this:
struct MyData {
string surname;
string forename;
bool operator<(const MyData& other) const {
return surname < other.surname || (surname==other.surname && forename < other.forename); }
};
This becomes pretty unmanageable for anything with more than 2 data members. Are there any simpler ways of achieving it? The data members may be any Comparable class.
#define LEX_LT(mem) if(mem != other.mem) return mem < other.mem
and then chain them asLEX_LT(surname); LEX_LT(forename); LEX_LT(var); ...; return false;
– Dube