Priority in pair inside priority_queue on basis of both elements I want a priority queue such that first it should be done on basis of first element (increasing order)and when clash occurs then on basis of second element(decreasing order). I came up with the following code :

#include&lt;bits/stdc++.h&gt;
using namespace std;


struct Compare {
    constexpr bool operator()(pair&lt;int, int&gt; const &amp; a,
                              pair&lt;int, int&gt; const &amp; b) const noexcept
    { return a.first &gt; b.first &amp;&amp; a.second&lt;b.second; }
};

int main()
{
    priority_queue&lt;pair&lt;int,int&gt;,
                   std::vector&lt;pair&lt;int,int&gt; &gt;,
                   Compare&gt; Q;
    int n;
    cin&gt;&gt;n;

    for(int i=0;i&lt;n;i++)
    {
        ll a,b;
        cin&gt;&gt;a&gt;&gt;b;
        Q.push(make_pair(a,b));
    }

    for(ll i=0;i&lt;n;i++)
    {
        pair&lt;ll,ll&gt; m;
        m=Q.top();
        cout&lt;&lt;m.first&lt;&lt;" "&lt;&lt;m.second&lt;&lt;"\n";
        Q.pop();
    }
}


For eg - If I have following pairs : (1,10),(1,7),(1,5),(2,8),(2,6),(3,5),(3,9)

My output should be as follows on calling Q.top() and Q.pop()

1 10
1 7
1 5
2 6
2 8
3 5
3 9


But instead I'm getting the following output:

1 10
1 5
3 9
1 7
2 8
2 6
3 5


Can someone help me out ?
This test is wrong:

return a.first &gt; b.first &amp;&amp; a.second&lt;b.second;


Try:

return a.first &gt; b.first || (a.first == b.first &amp;&amp; a.second &lt; b.second)


Some other things

This is not a legal header

#include&lt;bits/stdc++.h&gt;


Remove this immediately your code is invalid with it there.

Never do this:

using namespace std;


Its a bad habit that will cause you problems in real code. Always pre-fix things from the standard library with std:: (Its short for a reason). 

see: https://stackoverflow.com/q/1452721/14065

Use menaingful variable names.

Names like i n m have no meaning. Also when you have longer code searching the source for them will turn up a lot of false negatives thus making it harder to keep your code working.

Prefer prefix ++ over the postfix version.

    for(int i=0;i&lt;n;i++)

    for(int loop = 0; loop &lt; inputElementCount; ++loop)


A bit of white space makes things easier to read.

        cin&gt;&gt;a&gt;&gt;b;

        // Why not
        std::cin &gt;&gt; value1 &gt;&gt; value2;


When poping from a queue you should check its not empty not assume it has a particular size:

for(;!queue.empty(); queue.pop())
{
    auto const&amp; item = queue.top();
    std::cout &lt;&lt; item.first &lt;&lt; " " &lt;&lt; item.second &lt;&lt; "\n";
}