Priority_queue calls STL's Make_heap (), Pop_heap (), PUSH_HEAP () algorithm implementations, which is another form of heap. Write a priority_queue that is similar to the Priority_queue usage in the real STL using the STL heap algorithm 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 priority_queue in STL is similar in that it only adds templates and related iterators.
Priority_queue is relatively simple to use for basic types. His template declaration has three parameters: priority_queue<type, Container, functional> where type is the data type, Container is the container that holds the data, functional The method for comparing elements. Container must be an array-implemented container, such as a vector, deque but not a list.
In the STL, the vector is used by default. The comparison is by default with operator<, so if you put the next two parameters by default, the priority queue is the Big Top heap, the team head element is the largest.
#include <iostream> #include <queue>using namespace Std;int main () { priority_queue<int> q; for (int i= 0; i<; ++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 generally have to take the template three parameters are taken in. The STL defines a functor Greater<>, and for a basic type you can declare a small top heap with this functor
#include <iostream> #include <queue>using namespace Std;int main () { priority_queue<int, vector< Int>, greater<int> > Q; for (int i= 0; i<; ++i) Q.push (rand ()); while (!q.empty ()) { cout << q.top () << Endl; Q.pop (); } GetChar (); return 0;}
For custom types, you must overload the operator< yourself or write your own copy function
#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<; ++i) Q.push (Node (rand (), Rand ())); while (!q.empty ()) { cout << q.top (). x << ' << q.top (). y << Endl; Q.pop (); } GetChar (); return 0;}
After the custom type overloads the operator<, the object can be declared with only one template parameter.
But it can't be declared like a basic type at this point
Priority_queue<node, Vector<node>, greater<node> >;
The reason is that greater<node> is not defined, and if you want to define it this way, you can do so as follows:
#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) ret Urn a.y> b.y; return a.x> b.x; }};int Main () { priority_queue<node, vector<node>, cmp> Q; for (int i= 0; i<; ++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
The use of Priority_queue