c++优先队列(priority_queue)用法详解

转自csdn的文章,仅做为学习笔记。原文连接:https://blog.csdn.net/weixin_36888577/article/details/79937886ios

 

普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。数组

在优先队列中,元素被赋予优先级。当访问元素时,具备最高优先级的元素最早删除。优先队列具备最高级先出 (first in, largest out)的行为特征。数据结构

 

首先要包含头文件#include<queue>, 他和queue不一样的就在于咱们能够自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队。less

优先队列具备队列的全部特性,包括队列的基本操做,只是在这基础上添加了内部的一个排序,它本质是一个堆实现的。ide

和队列基本操做相同:函数

  • top 访问队头元素
  • empty 队列是否为空
  • size 返回队列内元素个数
  • push 插入元素到队尾 (并排序)
  • emplace 原地构造一个元素并插入队列
  • pop 弹出队头元素
  • swap 交换内容

定义:priority_queue<Type, Container, Functional>
Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,好比vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式。学习

当须要用自定义的数据类型时才须要传入这三个参数,使用基本数据类型时,只须要传入数据类型,默认是大顶堆。
通常是:spa

1 //升序队列
2 priority_queue <int,vector<int>,greater<int> > q;
3 //降序队列
4 priority_queue <int,vector<int>,less<int> >q;
5 
6 //greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了相似函数的行为,就是一个仿函数类了)

 

一、基本类型优先队列的例子:.net

 1 #include<iostream>
 2 #include <queue>
 3 using namespace std;
 4 int main() 
 5 {
 6     //对于基础类型 默认是大顶堆
 7     priority_queue<int> a; 
 8     //等同于 priority_queue<int, vector<int>, less<int> > a;
 9     
10     //      这里必定要有空格,否则成了右移运算符↓↓
11     priority_queue<int, vector<int>, greater<int> > c;  //这样就是小顶堆
12     priority_queue<string> b;
13 
14     for (int i = 0; i < 5; i++) 
15     {
16         a.push(i);
17         c.push(i);
18     }
19     while (!a.empty()) 
20     {
21         cout << a.top() << ' ';
22         a.pop();
23     } 
24     cout << endl;
25 
26     while (!c.empty()) 
27     {
28         cout << c.top() << ' ';
29         c.pop();
30     }
31     cout << endl;
32 
33     b.push("abc");
34     b.push("abcd");
35     b.push("cbd");
36     while (!b.empty()) 
37     {
38         cout << b.top() << ' ';
39         b.pop();
40     } 
41     cout << endl;
42     return 0;
43 }
View Code

运行结果:code

4 3 2 1 0
0 1 2 3 4
cbd abcd abc
请按任意键继续. . .

二、用pair作优先队列元素的例子:

规则:pair的比较,先比较第一个元素,第一个相等比较第二个。

 1 #include <iostream>
 2 #include <queue>
 3 #include <vector>
 4 using namespace std;
 5 int main() 
 6 {
 7     priority_queue<pair<int, int> > a;
 8     pair<int, int> b(1, 2);
 9     pair<int, int> c(1, 3);
10     pair<int, int> d(2, 5);
11     a.push(d);
12     a.push(c);
13     a.push(b);
14     while (!a.empty()) 
15     {
16         cout << a.top().first << ' ' << a.top().second << '\n';
17         a.pop();
18     }
19 }
View Code

运行结果:

2 5
1 3
1 2
请按任意键继续. . .

三、用自定义类型作优先队列元素的例子

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 
 5 //方法1
 6 struct tmp1 //运算符重载<
 7 {
 8     int x;
 9     tmp1(int a) {x = a;}
10     bool operator<(const tmp1& a) const
11     {
12         return x < a.x; //大顶堆
13     }
14 };
15 
16 //方法2
17 struct tmp2 //重写仿函数
18 {
19     bool operator() (tmp1 a, tmp1 b) 
20     {
21         return a.x < b.x; //大顶堆
22     }
23 };
24 
25 int main() 
26 {
27     tmp1 a(1);
28     tmp1 b(2);
29     tmp1 c(3);
30     priority_queue<tmp1> d;
31     d.push(b);
32     d.push(c);
33     d.push(a);
34     while (!d.empty()) 
35     {
36         cout << d.top().x << '\n';
37         d.pop();
38     }
39     cout << endl;
40 
41     priority_queue<tmp1, vector<tmp1>, tmp2> f;
42     f.push(b);
43     f.push(c);
44     f.push(a);
45     while (!f.empty()) 
46     {
47         cout << f.top().x << '\n';
48         f.pop();
49     }
50 }
View Code

运行结果:

3
2
1

3
2
1
请按任意键继续. . .