I am trying to learn about custom comparators in priority queue. however, i am unable to get the expected behaviour. I have wrote two custom comparators in the below code one for a vector and the other for a priority queue and the logic is same for both the comparators. how the output seems to be opposite, can anyone tell me the reason for this, any help will be appreciated.
In my thinking the output given by the priority queue should be same as the vector but it isn't.
Actual Ouput:
1 6 2 5
2 4
2 4 2 5 1 6
Expected Output:
1 6 2 5
2 4
1 6 2 5 2 4
Code:
include
include <bits/stdc++.h>
using namespace std;
define pii pair<int,int>
struct cmp1 { bool operator()(pii const& a,pii const& b) { if(a.first==b.first) return a.second>b.second; return a.first<b.first; } };
bool cmp2(pii const& a,pii const& b) { if(a.first==b.first) return a.second>b.second; return a.first<b.first; }
int main() { priority_queue<pii,vector,cmp1> cust_heap; cust_heap.push({1,6}); cust_heap.push({2,4}); cust_heap.push({2,5});
vector<pii> v; v.push_back({1,6}); v.push_back({2,4}); v.push_back({2,5}); sort(v.begin(),v.end(),cmp2); for(auto x:v) cout<<x.first<<" "<<x.second<<'\n'; cout<<"-----------------------------------------------"<<'\n'; while(!cust_heap.empty()) { int freq=cust_heap.top().first; int element=cust_heap.top().second; cust_heap.pop(); cout<<freq<<" "<<element<<'\n'; } cout<<'\n'; return 0;
}