Header file # include <queue>
Queue <int> q;
Q. push (12 );
While (! Q. empty ())
{
Cout <q. front () <endl;
Q. pop ();
}
Priority_queue <int> q; // Sort by size to size by default
Q. push (1 );
Q. push (2 );
While (! Q. empty ())
{
Cout <q. top () <endl;
Q. pop ();
}
Priority_queue <int, vector <int>, greater <int> q; // in ascending order
Q. push (23 );
Q. push (22 );
Q. push (2 );
While (! Q. empty ())
{
Cout <q. top () <endl;
Q. pop ();
}
For custom objects, you must perform the comparison operation on your own.
Struct Score {int score _; string name _; Score (int score, const string name): score _ (score), name _ (name ){}}; class Cmp {public: bool operator () (const Score & s1, const Score & s2) {return s1.score _ <s2.score _ ;}}; // Cmp p; // p (s1, s2) int main (int argc, const char * argv []) {priority_queue <Score, vector <Score>, Cmp> q; q. push (Score (67, "zhangsan"); q. push (Score (88, "lisi"); q. push (Score (34, "wangwu "));
}
Queue/priority_queue notes