3

Suppose I have a class with several member variables:

class MyClass{
    std::string a;
    int b;
    SomeOtherClass c;
    // some stuff...
public:
    // some other stuff...
};

I want to define relational operators (operator<, etc.) that first compare a, but if the a are equal, compare b, but if the b are equal, compare c. (We assume SomeOtherClass already has relational operators defined.) So I have something like

bool operator==(MyClass param){
    return (a == param.a) && (b == param.b) && (c == param.c);
}

bool operator<(MyClass param){
    if(a < param.a) return true;
    if(a > param.a) return false;
    if(b < param.b) return true;
    if(b > param.b) return false;
    if(c < param.c) return true;
    return false;
}

and so on. Is there any more elegant way to do this? It seems quite cumbersome, especially if there are lots of member variables to be compared. (Boost is an option.)

jcai
  • 3,448
  • 3
  • 21
  • 36

3 Answers3

7

Yes, there's two ways I've seen commonly:

bool operator<(MyClass param){
    if(a != param.a) return a<param.a;
    if(b != param.b) return b<param.b;
    return c<param.c;
} 

http://coliru.stacked-crooked.com/view?id=dd70799c005e6e99c70ebda552161292-c96156d6cc95286981b0e9deef2eefae

or

bool operator<(MyClass param){
    return std::tie(a, b, c)<std::tie(param.a, param.b, param.c);
}

http://coliru.stacked-crooked.com/view?id=00278eaca0d73b099fcd8edf87b5057b-c96156d6cc95286981b0e9deef2eefae

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
3

Sure, you can use std::tie for this:

#include <tuple>

bool operator<(const MyClass& lhs, const MyClass& rhs)
{
  return std::tie(lhs.a, lhs.b, lhs.c) < std::tie(rhs.a, rhs.b, rhs.c);
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
3

Of course, you can use std::tie :

#include <tuple>

bool operator<(MyClass param){
    return std::tie( a, b, c ) < std::tie( param.a, param.b, param.c );
}

It will create a tuple and after that, you just use the operator<.

This operator will compare each elements of the tuple.

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62