Why such strange behaviour of priority queue size() function.

Revision en3, by vintage_Petr_Mitrichev, 2019-06-13 00:28:21

Why such strange behaviour while outputting the difference in sizes of two queues. When i am storing them in a diff variable its showing right answer, while just outputting the difference gives wrong answer.

code

#include<bits/stdc++.h>
using namespace std;
int main()
{
    priority_queue<int , vector<int> , greater<int> > pmin;
    priority_queue<int , vector<int> >pmax;
    pmin.push(5);
    pmin.push(9);
    pmin.push(10);
    pmax.push(1);
    cout << pmax.size() - pmin.size() << endl;
    int diff = pmax.size() - pmin.size();
    cout << diff <<endl;
    return 0 ;
}

**** Since downvotes doesn't affects me as most of them know why, but I think this is a very good question whom most of have no idea about the flaw in this. So it would be good if some great answers would be here.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en7 English vintage_Petr_Mitrichev 2019-06-13 00:34:52 8 Tiny change: '\n****\n**Since down' -> '\n****\n**TO DV : Since down'
en6 English vintage_Petr_Mitrichev 2019-06-13 00:34:11 25 Tiny change: 'affects me as most of them know why, but I th' -> 'affects me, but I th'
en5 English vintage_Petr_Mitrichev 2019-06-13 00:31:45 20 Tiny change: 'd answers would be here.**' -> 'd answers known to other people.**'
en4 English vintage_Petr_Mitrichev 2019-06-13 00:30:54 7 Tiny change: ' if some great answers w' -> ' if some good answers w'
en3 English vintage_Petr_Mitrichev 2019-06-13 00:28:21 216
en2 English vintage_Petr_Mitrichev 2019-06-12 23:32:49 71
en1 English vintage_Petr_Mitrichev 2019-06-12 23:32:15 640 Initial revision (published)