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.
You could store the data in a
boost::tuple
, which provides lexicographic comparison, and provide named accessor functions, along the lines of:I believe this is also available as
std::tr1::tuple
, and will bestd::tuple
in the forthcoming standard.Maintaining the list of accessors is probably more manageable than maintaining the comparison code.
With the advent of C++11 there's a new and concise way to achieve this using std::tie:
tuple
is a good idea, but if you want to keep having names for your member variables, it might be good enough to restructure your comparison function like this:Depending on your taste, you might even want a macro like
#define COMPARE(field) if (field != other.field) return field < other.field;
to reduce duplication. Then the function would just become a list ofCOMPARE
-invocations.If all members have the same type you could put them in
std::vector
. By defaultstd::lexicographical_compare
will be used to compare vectors.You can use a
boost::tuple
orstd::pair
which has built-in lexigraphical comparison. Of course the disadvantage is you can't associate a method to the tuples.