according to my logic, this comparator will sort the pair based on the value stored in second (increasing) order, so
10,2 --> 1,5 --> 1,10 --> 7,5 --> 5,15. my answer is 10,2 will be on the top but it is showing 15,5 reverse of it.
please help me
Difficulty in understanding comparator function
send ur code si that i can check
#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<pi>, 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;
}
see ankit , according to me 10,2 will be the pair that is on the top of the priority queue
this question if from c++ STL quiz , ques no 11
The above code will sort the pair in following manner:
if the second of pair is unequal then sort the pair in descending order according to second
else it will sort the pair according to first in ascending order