I. Overview
Priority_queue. First, it is a queue, that is, only elements can be added at the low end and elements can be retrieved from the top. There is no other way to access elements (therefore, priority_queue does not provide the traversal function, it also does not provide an iterator); once again, it has priority, that is, the elements in the queue have a certain priority: the elements in it are automatically arranged according to the element's weight value, the highest weight, ranked first.
Note: The queue is not arranged in strict descending order of the weights, but is maintained every time the top (to the right) element is the element with the highest weight in the queue (which is implemented using heap internally ).
II. Implementation
Because priority_queue is based entirely on the bottom container and heap processing rules are added, the implementation is simple and the vector is the bottom container by default.
The definition of the contact adapter: A person who has the nature of this [modifying something interface to form another style] is called the adapter. STL priority_queue is classified as container adapter.
The source code of priority_queue in its SGI (Silicon Graphics Computer Systems, Inc.) STL is as follows:
Template <class T, class Sequeue = vector <T>, class Compare = less <typename Sequeue: value_type> class priority_queue {public: typedef typename Sequeue: value_type; typedef typename Sequeue: size_type; typedef typename Sequeue: reference; typedef typename Sequeue: const_reference; protected: Sequeue c; // underlying container Compare comp; // element size comparison standard public: priority_queue (): c () {} explicit priority_queue (const Compare & x): c (), comp (x) {} template <class InputIterator> priority_queue (InputIterator first, InputIterator last, const Compare & x): c (first, last), comp (x) {make_heap (c. begin (), c. end (), comp);} priority_queue (InputIterator first, InputIterator last) // compare the standard with the default comp: c (first, last) {make_heap (c. begin (), c. end (), comp);} bool empty () const {return c. empty ();} size_type size () const {return c. size ();} const_reference top () const {return c. front ();} void push (const value_type & x) {_ STL_TRY {c. push_back (x); // Add the container (vector) push_heap (c. begin (), c. end (), comp); // sort containers by Using heap} _ STL_UNWIND (c. clear ();} void pop () {--- STL_TRY {pop_heap (c. begin (), c. end (), comp); // exit heap (SORT) c. pop_back (); // Delete} _ STL_UNWIND (c. clear ());}};
Iii. test instances
#include <iostream> #include <queue> using namespace std; class myComparison { bool reverse; public: myComparison(const bool& revParam = false) { reverse = revParam; } bool operator()(const int& lhs, const int& rhs) const { if(reverse) return (lhs > rhs); else return (lhs < rhs); } }; int main() { int myInts[] = {10, 60, 50 ,20}; priority_queue<int> first; priority_queue<int> second(myInts, myInts+4); cout << "second size: " << second.size() << endl; cout << "second top: " << second.top() << endl; second.push(100); cout << "second top: " << second.top() << endl; priority_queue<int, vector<int>, greater<int> > third(myInts, myInts+4); cout << "third size: " << third.size() << endl; cout << "third top: " << third.top() << endl; third.push(100); cout << "third top: " << third.top() << endl; //using myComparison priority_queue<int, vector<int>, myComparison > fourth; typedef priority_queue<int, vector<int>, myComparison> myPq_type; myPq_type fifth(myComparison() ); myPq_type sixth(myInts, myInts+4, myComparison(true) ); cout << "sixth top: " << sixth.top() << endl; sixth.pop(); cout << "sixth top: " << sixth.top() << endl; return 0; }
Output result: