概述
首先要包含头文件#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;// top最小
// 降序队列,大顶堆,小于当前节点要下沉
priority_queue <int, vector<int>, less<int>> q;// top最大
//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)
// 默认是less
priority_queue <int> q;
例题:
菜鸡思考:
这个题挺离谱的,但不难想到最简单的方法应该是维护两个数组,前一半,后一半。这样应该会比整体排序/插入
再寻找简单一点。但这么一想,其实只时刻需要知道前一个数组的最后一个以及后一个数组的最开始一个就可以了,这个时候就可以使用优先队列啦。
菜鸡代码:
class MedianFinder {
private:
// 升序,top最大
priority_queue<int> frontQueue;
// 降序,top最小
priority_queue<int, vector<int>, greater<int>> backQueue;
public:
/** initialize your data structure here. */
MedianFinder() {}
void addNum(int num) {
if (frontQueue.empty()) {//空,先放进去
frontQueue.push(num);
return;
}
if (backQueue.size() + 1 == frontQueue.size()) {// 有可能放完这个数字两个队列不是均分的了
if ((backQueue.empty() && frontQueue.top() > num) || num <= frontQueue.top()) {// 要放到前一半
backQueue.push(frontQueue.top());
frontQueue.pop();
frontQueue.push(num);
// 把前一半最大的拿出来放到后面
return;
}
if (num >= frontQueue.top()) {// 放到后一半
backQueue.push(num);
return;
}
} else {// 两个一样多
if (num <= backQueue.top()) {
// 放到前一半
frontQueue.push(num);
} else if (num > frontQueue.top()) {
// 放到后一半,保证前一半的数量大于等于后一半,所以先从后面拿一个出来给前面
frontQueue.push(backQueue.top());
backQueue.pop();
backQueue.push(num);
}
}
return;
}
double findMedian() {
if (frontQueue.empty()) {
return 0;
}
if (backQueue.empty() || frontQueue.size() == backQueue.size() + 1) {
return frontQueue.top();
}
if (frontQueue.size() == backQueue.size()) {
return ((double)frontQueue.top() + (double)backQueue.top()) / 2;
}
return 0;
}
};
进阶操作
struct重载运算符
结构体中重载<
符号,注意声明函数为const
,否则编译不会通过:
bool operator < (const node& n1) const {
return this->val < n1.val;
}
例题:
菜鸡思考
菜鸡代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
struct MyNode {
ListNode *node;
int val;
bool operator > (const MyNode &node) const {
return this->val > node.val;
}
};
ListNode* mergeKLists(vector<ListNode*>& lists) {
// 使用小顶堆,保证top最小,最先出队
priority_queue<MyNode, vector<MyNode>, greater<MyNode>> myQueue;
for (auto node : lists) {
if (node) {
myQueue.push({node, node->val});
}
}
ListNode *head = new ListNode, *tail = head;
while (!myQueue.empty()) {
// 每次最小的出队,链到链表后面,把出队后面一个元素入队
auto temp = myQueue.top();
myQueue.pop();
if (temp.node->next) {
myQueue.push({temp.node->next, temp.node->next->val});
}
tail->next = temp.node;
tail = tail->next;
}
return head->next;
}
};
最后
以上就是怕孤单唇彩为你收集整理的C++优先队列(priority_queue)基本操作基本使用的全部内容,希望文章能够帮你解决C++优先队列(priority_queue)基本操作基本使用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复