Priority_queue calls make_heap (), pop_heap (), and push_heap () in STL to implement the algorithm. This is another form of heap. First, write a priority_queue which is implemented using the heap Algorithm in STL and similar to the priority_queue usage in STL to deepen the understanding of priority_queue.
#include <iostream> #include <algorithm> #include <vector>
using namespace std;
class priority_queue { private: vector<int> data; public: void push( int t ){ data.push_back(t); push_heap( data.begin(), data.end()); } void pop(){ pop_heap( data.begin(), data.end() ); data.pop_back(); } int top() { return data.front(); } int size() { return data.size(); } bool empty() { return data.empty(); } };
int main() { priority_queue test; test.push( 3 ); test.push( 5 ); test.push( 2 ); test.push( 4 ); while( !test.empty() ){ cout << test.top() << endl; test.pop(); } return 0;
}
|
The format of priority_queue in STL is similar to this, but the template and related iterator are added.
Priority_queue is relatively simple to use for basic types. The template declaration has three parameters:
Priority_queue <type, container, functional>
Type indicates the data type, container indicates the container for storing data, and functional indicates the element comparison method.
The container must be a container implemented using arrays, such as vector and deque, but not list.
In STL, vector is used by default. Operator is used by default for comparison. Therefore, if you set the following two parameters by default,
A priority queue is a big top heap with the largest element in the head.
#include <iostream> #include <queue>
using namespace std;
int main(){ priority_queue<int> q; for( int i= 0; i< 10; ++i ) q.push( rand() ); while( !q.empty() ){ cout << q.top() << endl; q.pop(); } getchar(); return 0; }
|
If you want to use a small top heap, you must include all three parameters in the template.
STL defines a function like greater. for basic types, you can use this function to declare a small top heap.
#include <iostream> #include <queue>
using namespace std;
int main(){ priority_queue<int, vector<int>, greater<int> > q; for( int i= 0; i< 10; ++i ) q.push( rand() ); while( !q.empty() ){ cout << q.top() << endl; q.pop(); } getchar(); return 0; }
|
For a custom type, you must reload the operator yourself <or write the imitation function yourself.
#include <iostream> #include <queue>
using namespace std;
struct Node{ int x, y; Node( int a= 0, int b= 0 ): x(a), y(b) {} };
bool operator<( Node a, Node b ){ if( a.x== b.x ) return a.y> b.y; return a.x> b.x; }
int main(){ priority_queue<Node> q; for( int i= 0; i< 10; ++i ) q.push( Node( rand(), rand() ) ); while( !q.empty() ){ cout << q.top().x << ' ' << q.top().y << endl; q.pop(); } getchar(); return 0; }
|
After the operator is reloaded for the custom type, only one template parameter can be included when the object is declared.
But it cannot be declared like the basic type.
Priority_queue <node, vector <node>, greater <node>;
The reason is that greater <node> is not defined. If you want to use this method to define it, you can use the following method:
#include <iostream> #include <queue>
using namespace std;
struct Node{ int x, y; Node( int a= 0, int b= 0 ): x(a), y(b) {} };
struct cmp{ bool operator() ( Node a, Node b ){ if( a.x== b.x ) return a.y> b.y; return a.x> b.x; } };
int main(){ priority_queue<Node, vector<Node>, cmp> q; for( int i= 0; i< 10; ++i ) q.push( Node( rand(), rand() ) ); while( !q.empty() ){ cout << q.top().x << ' ' << q.top().y << endl; q.pop(); } getchar(); return 0; }
// The above code implements a small top heap
|