Search
 
SCRIPT & CODE EXAMPLE
 

CPP

priority queue ordered by second element

#include <bits/stdc++.h>
using namespace std;

typedef pair<string, int> Max;
struct Compare {
    bool operator()(Max a, Max b) {
        return a.second < b.second;
    }
};

int main() {
    //Max heap custom data type
    priority_queue<Max, vector<Max>, Compare> p;
    p.push(make_pair("a", 1));
    p.push(make_pair("c", 1));
    p.push(make_pair("b", 3));

    while (!p.empty()) {
        Max top = p.top();
        cout << top.first << " => " << top.second << "
";
        p.pop();
    }
    /*
    * OUTPUT:
    * b = 3
    * a = 1
    * c = 1
    */
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: print hello world c++ 
Cpp :: c++ allocate and free dynamic 2d array 
Cpp :: c++ pi 
Cpp :: initialize 2d vector as 0 
Cpp :: chess perft 5 
Cpp :: leveling system c++ 
Cpp :: c++ edit another processes memory address 
Cpp :: C++ Kelvin to Celsius 
Cpp :: logisch und 
Cpp :: C++ Third angle of a Triangle 
Cpp :: shuffle elements c++ 
Cpp :: C++ std::async wait is taking forever 
Cpp :: gestd::getline with wstring 
Cpp :: invalid next size (normal) c++ 
Cpp :: set precision in c++ 
Cpp :: extern __shared__ memory 
Cpp :: distinct colors cses solution 
Cpp :: xmake set exe name 
Cpp :: prime number program 
Cpp :: how to display a variable in c++ 
Cpp :: resize two dimensional vector c++ 
Cpp :: cpp take lambda as parameter 
Cpp :: check if point is left or right of vector 
Cpp :: array 2d dynamic allocation c++ 
Cpp :: Unsorted Linked list in c++ 
Cpp :: how to check if a value is inside an array in c++ 
Cpp :: how to string to integer in c++ 
Cpp :: c++ string comparison 
Cpp :: how to put bitset into a string in c++ 
Cpp :: c++ char it is a number 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =