Sorting with C++ Struct

C++ struct can be sorted using lambda functions.

1
2
3
4
5
6
7
struct P {
  int x, y;
};

vector<P> ants(N);

sort(ants.begin(), ants.end(), [](P a, P b) {return p.x < p.x;});

C++ struct override operator function example below. Useful for sets and priority queues.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Point {
    ll x, y;
    int segindex;
    bool start;
};

bool operator< (Point p1, Point p2) { return p1.x==p2.x ? p1.y<p2.y : p1.x<p2.x; }

struct Segment {
    Point p, q;
    int index;
};

bool operator< (Segment s1, Segment s2) { return s1.index != s2.index && eval(s1)<eval(s2); }
bool operator== (Segment s1, Segment s2) { return s1.index == s2.index; }