#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
struct Compare
{
bool operator()(pi const & a, pi const & b)
{
if(a.second < b.second)
{
return true;
}
else if(a.second > b.second)
{
return false;
}
else
{
if(a.first > b.first)
{
return true;
}
return false;
}
}
};
int main()
{
priority_queue<pi , vector, Compare>q;
q.push({1, 5});
q.push({5, 15});
q.push({7, 15});
q.push({10, 2});
q.push({1, 10});
cout<<q.top().first<<" "<<q.top().second<<endl;
return 0;
}
How the comparison works in each call ?
How the comparison works in each call?
Refer this discussion Can you explain the output
and Need an explanation on the output...how the compare function is working?
Here, sorting of the pair occur according to the second of pair in decreasing order if the second of the pair are not equal. else sorting occur according to the first in increasing order.