Priority queue (forwarding) for STL containers and priority queue for stl containers
Priority queue of STL containers
Address: http://www.cnblogs.com/summerRQ/articles/2470130.html
Priority queue, which was used in the past when I used to brush questions, now I can only remember that its keyword is priority_queue (too hurt ). This data structure is useful in some scenarios where weights are defined.
First, review the definition of the queue: the queue maintains a group of objects, the objects entering the queue are placed at the end, and the next retrieved element is taken from the queue header. Priority_queue allows users to set priority for the elements stored in the queue. This type of queue does not directly place new elements at the end of the queue, but is placed before elements with lower priority than the queue. By default, the standard library uses the <operator to determine the priority relationship between objects. Therefore, to use a custom object, you need to overload the <operator.
There are two types of priority queues, one is the maximum priority queue; the other is the minimum priority queue; the first element retrieved from the queue each time is the highest priority and the smallest priority element.
1) definition of priority queue
Header file: "queue. h", "functional. h"
You can use an existing data structure with the default priority. You can also import a custom priority comparison object when defining the priority queue. Alternatively, you can use a custom object (data structure ), but the <operator must be reloaded.
2) Common Operations of the priority queue
Operations supported by priority queue |
Q. empty () if the queue is empty, true is returned; otherwise, false is returned. Q. size () returns the number of elements in the queue. Q. pop () deletes the first element, but does not return its value. Q. top () returns the element value with the highest priority, but does not delete this element. Q. push (item) inserts a new element in a priority-based position |
Q. top () is the search operation. Search for the element with the smallest priority in the minimum priority queue and the element with the highest priority in the maximum priority queue. Q. pop () is used to delete this element. Priority queue insertion and deletion of elements are complex O (lgn), so very fast
In addition, elements in the priority queue can have the same priority.
The following C example contains almost all common priority queue usage.
# Include <iostream> # include <functional> # include <queue> # include <vector> using namespace std; // define the comparison structure struct cmp1 {bool operator () (int & a, int & B) {return a> B; // minimum priority }}; struct cmp2 {bool operator () (int & a, int & B) {return a <B; // maximum value priority }}; // custom data structure struct number1 {int x; bool operator <(const number1 &) const {return x>. x; // minimum value first }}; struct number2 {int x; bool operator <(const number2 & a) const {ret Urn x <. x; // maximum value priority }}; int a [] = {,}; number1 num1 [] =, ,}; number2 num2 [] = {,}; int main () {priority_queue <int> que; // use the default priority to construct the queue priority_queue <int, vector <int>, cmp1> que1; // the minimum value is priority_queue <int, vector <int>, cmp2> que2; // The maximum value is priority_queue <int, vector <int>, greater <int> que3; // note that ">" is considered an error, priority_queue <int, vect Or <int>, less <int> que4; /// maximum priority priority_queue <number1> que5; // minimum priority queue priority_queue <number2> que6; // maximum priority queue 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]);} for (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 ("using the 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 () ;}puts (""); return 0 ;}/ * running result: use default priority relationship: (priority_queue <int> que;) Queue 0: 91 83 72 56 47 36 22 14 10 7 3 use struct custom priority mode 1: (priority_queue <int, vector <int>, cmp> que;) Queue 7 10 14 22 36 47 56 83 91 Queue 2: 91 83 72 56 47 36 22 14 10 7 3 use the header file "functional" to define the priority: (priority_queue <int, vector <int>, greater <int>/less <int> que;) Queue :3 3 7 10 14 22 36 47 56 83 91 Queue 4: 91 83 72 56 47 36 22 14 10 7 3 using struct custom priority mode 2: (priority_queue <number> que) Queue 3 7 10 14 22 36 47 56 83 91 Queue 6: 91 83 72 56 47 36 22 14 10 7 3 */