Priority Queue
Introduce
Priority queue is a special kind of queue that you know when sorting the learning heap. hit "View.
So what is the priority queue?
To put it plainly, it's a powerful queue.If you don't know the queue well, look at me This blog.
Where is it powerful?
Four words: automatic sorting.
Header file of priority queue &&declaration
First, you need
#include<queue>
using namespace std;
- 1
- 2
These two header files.
Secondly, the basic format of a priority queue declaration is:
Priority_queue<structure type>queue name;
For example:
priority_queue <int> i;
priority_queue <double> d;
- 1
- 2
However, the most common types are:
priority_queue <node> q;
//node is a structure
//Structures overloaded with'<'less than sign
priority_queue <int,vector<int>,greater<int> > q;
//No #include<vector>header file is required
//Note that the last two'>'should not be written together,'>' is the right shift operator
priority_queue <int,vector<int>,less<int> >q;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
We will talk about the differences in these declarations below.
Basic operations for priority queues
Same basic operation as queue.
If you want to know click here , see an introduction to queues.
Take a priority queue named q for example.
q.size();//Returns the number of elements in q
q.empty();//Returns if q is empty, empty returns 1, otherwise returns 0
q.push(k);//Insert k at the end of q
q.pop();//Delete the first element of q
q.top();//Returns the first element of q
q.back();//Returns the end element of q
- 1
- 2
- 3
- 4
- 5
- 6
Characteristics of priority queues
As mentioned above, automatic sorting.
What is the arrangement?
Here's how:
Default priority queue (non-structured)
priority_queue <int> q;
- 1
What is such a priority queue?Let's write a program to verify it.
#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int> q;
int main()
{
q.push(10),q.push(8),q.push(12),q.push(14),q.push(6);
while(!q.empty())
printf("%d ",q.top()),q.pop();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
The program intends to insert 10, 8, 12, 14, 6 in this priority queue and then output.
What is the result?
14 12 10 8 6
That is, it is sorted from big to small!
Default priority queue (structure, overload less than)
Let's first see what this structure is.
struct node
{
int x,y;
bool operator < (const node & a) const
{
return x<a.x;
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
This node structure has two members, X and y, and its less-than rule is that x is smaller than x.
Let's look at the validation program again:
#include<cstdio>
#include<queue>
using namespace std;
struct node
{
int x,y;
bool operator < (const node & a) const
{
return x<a.x;
}
}k;
priority_queue <node> q;
int main()
{
k.x=10,k.y=100; q.push(k);
k.x=12,k.y=60; q.push(k);
k.x=14,k.y=40; q.push(k);
k.x=6,k.y=80; q.push(k);
k.x=8,k.y=20; q.push(k);
while(!q.empty())
{
node m=q.top(); q.pop();
printf("(%d,%d) ",m.x,m.y);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
The program intends to insert (10,100), (12,60), (14,40), (6,20), (8,20) these five node s.
Look at its output again:
(14,40) (12,60) (10,100) (8,20) (6,80)
It is also sorted from large to small according to the less-than rule after overloading.
less and greater priority queues
Or, let's start with int:
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
- 1
- 2
Say nothing more. Procedures and results:
#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int,vector<int>,less<int> > p;
priority_queue <int,vector<int>,greater<int> > q;
int a[5]={10,12,14,6,8};
int main()
{
for(int i=0;i<5;i++)
p.push(a[i]),q.push(a[i]);
printf("less<int>:")
while(!p.empty())
printf("%d ",p.top()),p.pop();
pritntf("\ngreater<int>:")
while(!q.empty())
printf("%d ",q.top()),q.pop();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
Result:
less<int>:14 12 10 8 6
greater<int>:6 8 10 12 14
So, we know that less is from big to small and great is from small to large.
Make a summary
In order to install 13 conveniently, we recommend you to write:
priority_queue<int,vector<int>,less<int> >q;
priority_queue<int,vector<int>,greater<int> >q;
- 1
- 2
Ordinarily, if you use vector <int>, less <int> from the beginning to the end, you may change from the beginning to the end, and you will forget how to write greater <int> and you will lose it.
summary
The priority queue is summarized here.
In fact, whether it is a queue or a priority queue, there is more to explore than just what I said.
Learning is endless.