By default, priority queue containers are implemented using vector containers. You can also use double-end queue containers. Priority queue always puts the highest priority element at the forefront of the queue to maintain the order of the queue.
The insert Operation PUSH () uses a double-variable Boolean function to re-sort the elements in the queue to meet this requirement.
This function can be provided by the user. Otherwise, the <operator can be used. The greater the element, the higher the priority.
If the element is smaller and the priority is higher, you need to use the function object greater, indicating that when you decide to insert a new element to the priority queue, the push () operator should be> rather than <.
Member functions:
Empty |
True if the priority queue has no elements |
Pop |
Removes the top element of a priority queue |
Push |
Adds an element to the end of the priority queue |
Size |
Returns the number of items in the priority queue |
Top |
Returns the top element of the priority queue |
In the priority queue, the element with the highest priority is the first-out queue. By default, the standard library uses the <operator of the element type to determine the priority relationship between them.
The first and most common usage of priority queue:
priority_queue<int> qi;
The <operator indicates that the priority of a large element in an integer is high.
The output result in Example 1 is: 9 6 5 3 2
Method 2:
In example 1, what if we want to output elements from small to large?
In this case, we can pass in a comparison function using the functional. H function object as the comparison function.
priority_queue<int, vector<int>, greater<int> > qi2;
Where
The second parameter is the container type.
The third parameter is the comparison function.
The output result in example 2 is: 2 3 5 6 9
Method 3: Customize the priority.
struct node{ friend bool operator< (node n1, node n2) { return n1.priority < n2.priority; } int priority; int value;};
In this structure, value is the value and priority is the priority.
You can use the custom operator <operator to compare the priority of an element.
In Example 3, the output result is:
Priority Value
9 5
8 2
6 1
2 3
1 4
However, if the structure is defined as follows:
struct node{ friend bool operator> (node n1, node n2) { return n1.priority > n2.priority; } int priority; int value;};
It will not compile (G ++ compiler) because the standard library uses the element type <operator by default to determine the priority relationship between them. The <operator and> operator of the custom type is not directly related, so it cannot be compiled.
// Code list
# Include <iostream> # include <functional> # include <queue> using namespace STD; struct node {friend bool operator <(node N1, node N2) {return n1.priority <n2.priority ;} int priority; int value ;}; int main () {const int Len = 5; int I; int A [Len] = {3, 5, 9, 6, 2 }; // example 1priority_queue <int> qi; for (I = 0; I <Len; I ++) Qi. push (A [I]); for (I = 0; I <Len; I ++) {cout <Qi. top () <"; Qi. pop () ;}cout <Endl; // example 2priority_queue <int, vector <int>, greater <int> qi2; for (I = 0; I <Len; I ++) qi2.push (A [I]); for (I = 0; I <Len; I ++) {cout <qi2.top () <""; qi2.pop () ;}cout <Endl; // example 3priority_queue <node> Qn; Node B [Len]; B [0]. priority = 6; B [0]. value = 1; B [1]. priority = 9; B [1]. value = 5; B [2]. priority = 2; B [2]. value = 3; B [3]. priority = 8; B [3]. value = 2; B [4]. priority = 1; B [4]. value = 4; for (I = 0; I <Len; I ++) Qn. push (B [I]); cout <"Priority" <'\ t' <"value" <Endl; for (I = 0; I <Len; I ++) {cout <Qn. top (). priority <'\ t' <Qn. top (). value <Endl; Qn. pop () ;}return 0 ;}
In addition, paste the code used by the priority queue:
# Include <iostream> # include <functional> # include <queue> # include <vector> using namespace STD; struct CMP1 {bool operator () (Int & A, Int & B) {return A> B; // sorting from small to large, with a high priority}; struct cmp2 {bool operator () (Int & A, Int & B) {return a <B; // from large to small}; struct number1 {int X; bool operator <(const number1 & A) const {return x>. x; // from small to large, X small priority high }}; struct number2 {int X; bool operator <(const number2 & A) con St {return x <. x; // from large to small, X high priority level}; int A [] = }; number1 num1 [] = {,}; number2 num2 [] = {, 47 }; int main () {priority_queue <int> que; // uses the default priority to construct a queue from large to small. Priority_queue <int, vector <int>, CMP1> que1; priority_queue <int, vector <int>, cmp2> que2; priority_queue <int, vector <int>, greater <int> que3; // priority_queue <int, vector <int>, less <int> que4; // priority_queue <number1> que5; priority_queue <number2> que6; int I; for (I = 0; A [I]; I ++) {que. push (A [I]); que1.push (A [I]); que2.push (A [I]); que3.push (A [I]); que4.push (A [I]);} (I = 0; num1 [I]. x; I ++) que5.push (num1 [I]); for (I = 0; num2 [I]. x; I ++) que6.push (num2 [I]); printf ("default priority: \ n (priority_queue <int> que;) \ n "); printf ("queue 0: \ n"); While (! Que. empty () {printf ("% 3d", que. top (); que. pop ();} puts (""); puts (""); printf ("using struct custom priority mode 1: \ n (priority_queue <int, vector <int>, CMP> que;) \ n "); printf (" queue 1: \ n "); While (! Que1.empty () {printf ("% 3d", que1.top (); que1.pop () ;}puts (""); printf ("queue 2: \ n "); while (! Que2.empty () {printf ("% 3d", que2.top (); que2.pop () ;}puts (""); puts (""); printf ("use header file \" functional \ "to define the priority: \ n (priority_queue <int, vector <int>, greater <int>/less <int> que ;) \ n "); printf (" queue 3: \ n "); While (! Que3.empty () {printf ("% 3d", que3.top (); que3.pop () ;}puts (""); printf ("queue 4: \ n "); while (! Que4.empty () {printf ("% 3d", que4.top (); que4.pop () ;}puts (""); puts (""); printf ("using struct custom priority mode 2: \ n (priority_queue <number> que) \ n"); printf ("queue 5: \ n"); While (! Que5.empty () {printf ("% 3d", que5.top (); que5.pop () ;}puts (""); printf ("queue 6: \ n "); while (! Que6.empty () {printf ("% 3d", que6.top (); que6.pop () ;}return 0 ;}
Execution result:
September Netease youdao recruitment questions:
The array with the size of N is given, and the fastest way is to find the large number of the first M.
# Include <iostream> # include <functional> # include <queue> using namespace STD; int main () {priority_queue <int, vector <int>, greater <int> q; // int n, m, num; while (CIN> N> m) {int I; for (I = 0; I <m; I ++) {CIN> num; q. push (Num) ;}for (; I <n; I ++) {CIN> num; If (Num> q. top () {q. pop (); q. push (Num) ;}} while (! Q. Empty () {cout <q. Top () <""; q. Pop () ;}cout <Endl ;}return 0 ;}
This topic can be implemented using a minimum heap with a size of M. After the initial heap is created using m before the array, if the element a [I] Behind is greater than the heap top element, delete the heap top and insert new elements.
References:
Http://www.cppblog.com/shyli/archive/2007/04/06/21366.html
Http://blog.csdn.net/yidujinhuang/article/details/6868093
Http://blog.csdn.net/hopeztm/article/details/8026457