comparison class usage in priority_queue

There is a way to use customized comparison function in C++ priority_queue. Instead of reloading the ‘less than’ operator, one can also write a compare class in which the ‘()’ operator is reloaded when defining. Here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<bits/stdc++.h>
using namespace std;

class pts {
public:
int x,y;
};

class comp {
public:
bool operator() (pts a, pts b){
return a.x < b.x;
}
};

int main() {
priority_queue<pts, vector<pts>, comp> s;
return 0;
}