1//Sol1
2PriorityQueue<Integer> queue = new PriorityQueue<>(10, Collections.reverseOrder());
3
4
5//Sol2
6// PriorityQueue<Integer> pq = new PriorityQueue<>((x, y) -> y - x);
7PriorityQueue<Integer> pq =new PriorityQueue<>((x, y) -> Integer.compare(y, x));
8
9
10//Sol3
11PriorityQueue<Integer> pq = new PriorityQueue<Integer>(defaultSize, new Comparator<Integer>() {
12 public int compare(Integer lhs, Integer rhs) {
13 if (lhs < rhs) return +1;
14 if (lhs.equals(rhs)) return 0;
15 return -1;
16 }
17});
1#include <bits/stdc++.h>
2using namespace std;
3
4// User defined class, Point
5class Point
6{
7 int x;
8 int y;
9public:
10 Point(int _x, int _y)
11 {
12 x = _x;
13 y = _y;
14 }
15 int getX() const { return x; }
16 int getY() const { return y; }
17};
18
19// To compare two points
20class myComparator
21{
22public:
23 int operator() (const Point& p1, const Point& p2)
24 {
25 return p1.getX() > p2.getX();
26 }
27};
28
29// Driver code
30int main ()
31{
32 // Creates a Min heap of points (order by x coordinate)
33 priority_queue <Point, vector<Point>, myComparator > pq;
34
35 // Insert points into the min heap
36 pq.push(Point(10, 2));
37 pq.push(Point(2, 1));
38 pq.push(Point(1, 5));
39
40 // One by one extract items from min heap
41 while (pq.empty() == false)
42 {
43 Point p = pq.top();
44 cout << "(" << p.getX() << ", " << p.getY() << ")";
45 cout << endl;
46 pq.pop();
47 }
48
49 return 0;
50}