#include <the iostream> #include <Queue> the using namespace STD; int main () { // for a base type default heap big top The priority_queue < int > A; // equivalent priority_queue <int, vector <int> , less < int>> A; // there must be a space, or become a right-shift operator ↓ The priority_queue < int , Vector < int >, Greater < int >> C; // this is a small top stack The priority_queue < String > B; for ( int I = 0 ; I < . 5 ; I ++) { a.push(i); c.push(i); } while (!a.empty()) { cout << a.top() << ' '; a.pop(); } cout << endl; while (!c.empty()) { cout << c.top() << ' '; c.pop(); } cout << endl; b.push("abc"); b.push("abcd"); b.push("cbd"); while (!b.empty()) { cout << b.top() << ' '; b.pop(); } cout << endl; return 0; }
Essence heap priority queue implementation;
The default definition of a large root heap priority queue, i.e. the value of the parent node is greater than the value of the child node.
The priority_queue <int> A;
// equivalent priority_queue <int, vector <int> , less <int>> a;
of course also be defined rootlets stack:
priority_queue <int, vector <int>, greater <int>> c; // this is a small stack top
The pair is added to the queue:
The priority_queue <pair <int, int>> A; // first comparator then compare the first second
operation with respect to the priority queue;
Access top the head elements
if the queue is empty empty
return the number of elements within a queue size
Push element is inserted into the tail (and ordering)
emplace situ element and configured to enqueue a
pop pop head elements
swap exchanging content
Can join behind from the queue, the queue out of the front, at the time of traversal by P.SIZE () or p.empty ();
excerpt from the blog from https://blog.csdn.net/weixin_36888577/article/details / 79937886