Priority_queue (queue similar) is completely based on the bottom container, plus the implementation principle of the binary heap (Dagen or small Gan), so its implementation is very simple, by default priority_queue vector as the bottom container. In addition, the default comparison rule for priority_queue is less:
class Compare = Less<typename sequence::value_type>
Less corresponds to implementing the priority queue according to Dagen, and of course the comparison rule can be set to greater, which is the priority queue implemented by Keng Gen.
Priority_queue (queue) completes all its work with the bottom container, which has the property of "modifying something interface to form another style", called an adapter (adapter). Therefore, priority_queue are often not categorized in containers (container), but instead are categorized in container adapters (container adapter). Priority_queue is a sequential container adapter, and the order container in STL also includes Queue,stack. These three sequential container adapters use the underlying sequential container to do the work they should do.
The above content is summarized from the "STL Source Analysis".
priority_queue Example:
#include <iostream>#include<vector>#include<queue>using namespacestd;intMain () {intia[9] = {0,1,2,3,4,8,9,3,5}; Priority_queue<int, vector<int, greater<int> > Ipq (ia, IA +9);//Compare for greater<int> description Implementation principle is small Gan, can be changed to less<int> observation outputcout <<"size ="<< ipq.size () <<Endl; for(inti =0; I < ipq.size (); ++i) {cout<< ipq.top () <<" "; } cout<<Endl; while(!Ipq.empty ()) {cout<< ipq.top () <<" "; Ipq.pop (); } cout<<Endl; return 0;}
Priority Queue Priority_queue in STL