priority queue ordered by second element

Solutions on MaxInterview for priority queue ordered by second element by the best coders in the world

showing results for - "priority queue ordered by second element"
Nico
09 Aug 2018
1#include <bits/stdc++.h>
2using namespace std;
3
4typedef pair<string, int> Max;
5struct Compare {
6    bool operator()(Max a, Max b) {
7        return a.second < b.second;
8    }
9};
10
11int main() {
12    //Max heap custom data type
13    priority_queue<Max, vector<Max>, Compare> p;
14    p.push(make_pair("a", 1));
15    p.push(make_pair("c", 1));
16    p.push(make_pair("b", 3));
17
18    while (!p.empty()) {
19        Max top = p.top();
20        cout << top.first << " => " << top.second << "\n";
21        p.pop();
22    }
23    /*
24    * OUTPUT:
25    * b = 3
26    * a = 1
27    * c = 1
28    */
29}