0

I have searched online for a while, but I still cannot figure out how the following one-line comparator works. I know inside the () are the parameters, but where is the return type bool? Is the name of the function omitted? What's the purpose of []?

/**
 * Definition for an interval.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
sort(intervals.begin(), intervals.end(), [](Interval& a, Interval& b){return a.start < b.start;});
hellow
  • 12,430
  • 7
  • 56
  • 79
Lusha Li
  • 1,068
  • 2
  • 11
  • 22
  • 3
    That is a [lambda expression](https://en.cppreference.com/w/cpp/language/lambda). It takes two arguments and compares them with the less then operator. – hellow Sep 25 '18 at 09:37
  • This won't work, as `Interval` does not overload the `<` operator. – Mike Borkland Sep 25 '18 at 11:10

0 Answers0