2
struct mycompare1 {
    bool operator() (const Interval a, const Interval b) {
        return a.start < b.start;
    }
} mycompare1_instance;

static bool mycompare2(const Interval a, const Interval b) {
    return a.start < b.start;
}

Q1: What is the difference between these two. It seems that mycompare1_instance somehow equals to mycompare2 like this. The following two lines seem to do the same thing.

sort(intervals.begin(), intervals.end(), mycompare1);
sort(intervals.begin(), intervals.end(), mycompare2);

Could anyone explain it?

Q2: How is "static" useful here?

Thx ahead!

user3692521
  • 2,563
  • 5
  • 27
  • 33

2 Answers2

1

The first case defines operator() so you could could use it as a function - in short they are functors. The advantage is that it can have a state and depending how you define the operator() you could use the instance of that class as a function. Thse second case is simply a static functionn and does not hold the state in the sense of a class/struct.

itachi
  • 192
  • 2
  • 10
0

I suspect the question is related to functors

ie you have seen

mycompare_instance1(x,y)

vs

mycompare2(x,y)

The first one is a functor. It used where we need to make things that are both objects with state and can be invoked like functions. The second one is a simple function

pm100
  • 48,078
  • 23
  • 82
  • 145