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

既然是队列那么先要包含头文件#include <queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队

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

和队列基本操作相同:

top 访问队头元素
empty 队列是否为空
size 返回队列内元素个数
push 插入元素到队尾 (并排序)
emplace 原地构造一个元素并插入队列
pop 弹出队头元素
swap 交换内容
定义:priority_queue<Type, Container, Functional>
Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆
一般是:

//升序队列
priority_queue <int,vector<int>,greater<int> > q;
//降序队列
priority_queue <int,vector<int>,less<int> >q;

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)
1
2
3
4
5
6
基本类型例子:
#include<iostream>
#include <queue>
using namespace std;
int main()
{
//对于基础类型 默认是大顶堆
priority_queue<int> a;
//等同于 priority_queue<int, vector<int>, less<int> > a;

// 这里一定要有空格,不然成了右移运算符↓
priority_queue<int, vector<int>, greater<int> > c; //这样就是小顶堆
priority_queue<string> b;

for (int i = 0; i < 5; i++)
{
a.push(i);
c.push(i);
}
while (!a.empty())
{
cout << a.top() << ' ';
a.pop();
}
cout << endl;

while (!c.empty())
{
cout << c.top() << ' ';
c.pop();
}
cout << endl;

b.push("abc");
b.push("abcd");
b.push("cbd");
while (!b.empty())
{
cout << b.top() << ' ';
b.pop();
}
cout << endl;
return 0;
}
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
输出

4 3 2 1 0
0 1 2 3 4
cbd abcd abc
1
2
3
2.pari的比较,先比较第一个元素,第一个相等比较第二个

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

2 5
1 3
1 2
1
2
3
3.对于自定义类型

#include <iostream>
#include <queue>
using namespace std;

//方法1
struct tmp1 //运算符重载<
{
int x;
tmp1(int a) {x = a;}
bool operator<(const tmp1& a) const
{
return x < a.x; //大顶堆
}
};

//方法2
struct tmp2 //重写仿函数
{
bool operator() (tmp1 a, tmp1 b)
{
return a.x < b.x; //大顶堆
}
};

int main()
{
tmp1 a(1);
tmp1 b(2);
tmp1 c(3);
priority_queue<tmp1> d;
d.push(b);
d.push(c);
d.push(a);
while (!d.empty())
{
cout << d.top().x << '\n';
d.pop();
}
cout << endl;

priority_queue<tmp1, vector<tmp1>, tmp2> f;
f.push(c);
f.push(b);
f.push(a);
while (!f.empty())
{
cout << f.top().x << '\n';
f.pop();
}
}
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
输出

3
2
1

3
2
1
---------------------
作者:吕白_
来源:优快云
原文:https://blog.youkuaiyun.com/weixin_36888577/article/details/79937886
版权声明:本文为博主原创文章,转载请附上博文链接!

转载于:https://www.cnblogs.com/lyp-Bird/p/10295305.html

<think>好的,用户想了解C++优先队列priority_queue用法,包括使用方法、示例、语法和解释。我需要根据提供的引用资料来整理回答。 首先,要确定priority_queue的基本用法。根据引用[1],priority_queueC++中需要包含头文件,但引用[4]提到头文件是<queue>,而引用[5]的例子也使用了#include <queue>,所以应该纠正引用[4]中的头文件错误。基本语法是priority_queue<类型, 容器, 比较函数>,默认使用vector作为容器,比较函数默认是less,形成大顶堆。 接下来,自定义比较函数部分。引用[1]和[2]提到可以使用仿函数或decltype来指定比较函数。例如,仿函数需要重载operator(),并且注意比较的逻辑,比如引用[2]中的例子用*a > *b实现小顶堆。同时,引用[3]展示了使用less和greater来创建大顶堆和小顶堆的不同方法。 然后,示例部分。引用[5]提供了基本的示例代码,使用默认的priority_queue,输出是按大顶堆排列。需要补充小顶堆的示例,以及使用自定义比较函数的例子,比如结构体或指针的比较。 需要注意语法中的细节,比如模板参数的位置,使用decltype时需要配合构造函数传递比较函数对象,如引用[1]中的例子。此外,在C++17之后可能可以省略模板参数,但为了兼容性最好明确指定。 用户可能的问题包括如何调整堆的类型、自定义数据结构的比较方法,以及为什么使用仿函数。需要确保回答覆盖这些点,并引用正确的资料。例如,引用[3]解释了大顶堆和小顶堆的不同声明方式,引用[2]展示了指针类型的比较方法。 最后,生成相关问题,可能涉及性能、应用场景或与其他容器的比较。需要确保问题与主题相关,并基于引用内容,比如比较函数的实现方式或优先队列的时间复杂度。 总结回答结构:基本用法、自定义比较函数的三种方法、示例代码、注意事项,然后相关问题。确保每个部分都有对应的引用支持,并正确标注引用标识。</think>### C++priority_queue用法详解 #### 1. 基本语法与默认行为 优先队列`priority_queue`是C++标准库提供的**堆数据结构**实现,包含在头文件`<queue>`中[^4][^5]。其默认行为为**大顶堆**(降序排列),声明语法为: ```cpp priority_queue<T, Container, Compare> pq; ``` - `T`:元素类型(如`int`、`string`) - `Container`:底层容器(默认`vector<T>`) - `Compare`:比较函数(默认`less<T>`,即大顶堆) 例如,创建整数大顶堆: ```cpp priority_queue<int> pq; // 等价于 priority_queue<int, vector<int>, less<int>> ``` #### 2. 自定义比较函数方法 ##### (1) 使用内置函数对象 通过`greater<T>`创建**小顶堆**(升序排列): ```cpp priority_queue<int, vector<int>, greater<int>> min_heap; // 小顶堆[^3] ``` ##### (2) 自定义仿函数(推荐) 定义含`operator()`的类,例如处理指针类型: ```cpp class Compare { public: bool operator()(int* a, int* b) { return *a > *b; // 小顶堆比较规则[^2] } }; priority_queue<int*, vector<int*>, Compare> ptr_heap; ``` ##### (3) 使用decltype与lambda表达式(C++11+) 适用于需要动态比较逻辑的场景: ```cpp auto cmp = [](int a, int b) { return a > b; }; priority_queue<int, vector<int>, decltype(cmp)> custom_heap(cmp); // 需传递lambda对象[^1] ``` #### 3. 完整示例代码 ##### 示例1:默认大顶堆 ```cpp #include <iostream> #include <queue> using namespace std; int main() { int arr[] = {3, 1, 4, 1, 5}; priority_queue<int> pq; for (int x : arr) pq.push(x); while (!pq.empty()) { cout << pq.top() << " "; // 输出:5 4 3 1 1 pq.pop(); } return 0; } ``` ##### 示例2:结构体排序 ```cpp struct Node { int value; Node(int v) : value(v) {} }; struct NodeCompare { bool operator()(const Node& a, const Node& b) { return a.value < b.value; // 按value降序排列 } }; priority_queue<Node, vector<Node>, NodeCompare> node_heap; ``` #### 4. 关键注意事项 1. **时间复杂度**:插入(`push`)和删除堆顶(`pop`)操作均为$O(\log n)$,访问堆顶(`top`)为$O(1)$[^3] 2. **元素类型要求**:比较函数需满足**严格弱序**关系 3. **容器选择**:底层容器必须支持随机访问迭代器(如`vector`或`deque`)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值