Priority queue of STL container
Priority queue, the previous brush the problem with the relatively ripe, now unexpectedly I can only remember its key word is priority_queue (too hurt). This data structure is useful in some places where weights are defined.
First review the definition of the queue: The queue maintains a set of objects, the objects that enter the queue are placed at the tail, and the next element is taken from the queue header. What's special about priority_queue is that it allows the user to prioritize the elements stored in the queue. Instead of placing the new element directly at the end of the queue, the queue is placed in front of the element that is lower than its priority. The standard library uses the < operator by default to determine the precedence relationship between objects, so if you want to use a custom object, you need to overload the < operator.
There are two priority queues, one is the maximum priority queue, the other is the smallest priority queue, and the first element taken from the queue is the element with the highest priority and the lowest priority, respectively.
1) Definition of priority queue
Include header file: "Queue.h", "functional.h"
You can either use an existing data structure with a default priority, or you can either pass in a custom priority comparison object when you define a priority queue, or use a custom object (data structure), but you must reload the good < operator.
2) Common operations for priority queues
Operations supported by the priority queue |
Q.empty () returns True if the queue is empty, otherwise false Q.size () returns the number of elements in the queue Q.pop () deletes the first element of the team, but does not return its value Q.top () returns the element value with the highest priority, but does not delete the element Q.push (item) inserts a new element in the appropriate priority-based location |
where Q.top () is the lookup operation, searching for the element with the least priority in the minimum priority queue, and searching for the element with the highest priority in the maximum priority queue. Q.pop () to delete the element. The complexity of inserting and deleting elements in the priority queue is O (LGN), so quickly
In addition, in the priority queue, elements can have the same precedence.
The following C example contains almost all the common priority queue usages.
#include <iostream>#include<functional>#include<queue>#include<vector>using namespacestd;//defining a comparison structurestructcmp1{BOOL operator()(int&a,int&b) { returna>b;//Minimum value First }};structcmp2{BOOL operator()(int&a,int&b) { returna<b;//Maximum Value First }};//Custom Data Structuresstructnumber1{intx; BOOL operator< (ConstNumber1 &a)Const { returnx>a.x;//Minimum value First }};structnumber2{intx; BOOL operator< (ConstNumber2 &a)Const { returnx<a.x;//Maximum Value First }};inta[]={ -,Ten, About,7, the, A, $, the,3, -, the,0};number1 num1[]={ -,Ten, About,7, the, A, $, the,3, -, the,0};number2 num2[]={ -,Ten, About,7, the, A, $, the,3, -, the,0};intMain () {priority_queue<int>que;//construct queues with default precedencepriority_queue<int,vector<int>,cmp1>que1;//Minimum value Firstpriority_queue<int,vector<int>,cmp2>que2;//Maximum Value Firstpriority_queue<int,vector<int>,greater<int> >que3;//Note that ">>" will be considered an error,priority_queue<int,vector<int>,less<int> >que4;////MAX Prioritypriority_queue<number1>que5;//Minimum priority queuepriority_queue<number2>que6;//Maximum priority queue inti; 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 ("take the default precedence relationship:/n (priority_queue<int>que;)/n"); printf ("Queue 0:/n"); while(!Que.empty ()) {printf ("%3d", Que.top ()); Que.pop (); } puts (""); Puts (""); printf ("adopt struct Custom Priority method one:/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/"priority within definition:/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 ("adopt struct Custom Priority mode two:/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;}/*Run Result: with default precedence: (priority_queue<int>que;) Queue 0:91 83 72 56 47 36 22 14 10 7 3 Adopt structure custom Priority mode one: (PRIORITY_QUEUE&L t;int,vector<int>,cmp>que;) Queue 1:3 7 + 91Queue 2:91 83 72 56 47 36 22 14 10 7 3 with header file "F Unctional "within defined priority: (priority_queue<int,vector<int>,greater<int>/less<int> >que;) Queue 3:3 7 91Queue 4:91 83 72 56 47 36 22 14 10 7 3 Adopt structure custom Priority Mode two: (priority_queue<number>que) Queue 5: 3 7 91Queue 6:91 -------7 3*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The priority queue for STL specific operations