Home > Back-end >  Priority_queue. Top () returns the value of doubt?
Priority_queue. Top () returns the value of doubt?

Time:10-02

 
#include
#include
using namespace std;

Int main ()
{
Priority_queue & lt; Int> Q;
For (int I=0; I & lt; 5; I++)
Q.p ush (I);

while(! Q.e mpty ())
{
Const int& Cur=q.t op (); Q.p op ();
STD: : cout & lt; }

return 0;
}


The above code print result: 3 2 1 0 0
If the const int& Cur=q.t op (); Modified to const int cur=q.t op (); Printing result is 4 3 2 1 0, normal,
Why have a problem with reference to receive?

CodePudding user response:

Because reference itself, namely cur=top when pop, cur pointing to his top now, so change
And the value of cur=top () is to replicate, pop, cur constant

CodePudding user response:

1/f, positive solutions, you this API design not good, can you guarantee you top no illegal operation? STD: : must judge is not empty to front (), don't pop_front. The meaning and you 2 above, if the empty cut in front can produce unpredictable errors,
  • Related