priority_queue
priority_queue
容器适配器定义了一个元素有序排列的队列。默认队列头部的元素优先级最高。因为它是一个队列,所以只能访问第一个元素,这也意味着优先级最高的元素总是第一个被处理。
| |
| template <typename T, typename Container=vector<T>, typename Compare=less<T>> class priority_queue |
priority_queue
实例默认有一个 vector
容器。函数对象类型 less<T>
是一个默认的排序断言,定义在头文件 function
中(其中还定义了 greater<T>
),决定了容器中最大的元素会排在队列前面。
| int main() { |
| vector<int> ary = {9, 5, 2, 7}; |
| |
| priority_queue<int> heap{begin(ary), end(ary)}; |
| |
| |
| priority_queue<int, vector<int>, greater<>> heap2{begin(ary), end(ary)}; |
| |
| |
| priority_queue<int, deque<int>, greater<>> heap3{begin(ary), end(ary)}; |
| } |

创建和初始化
| int main() { |
| |
| priority_queue<string> words; |
| |
| |
| string wrds[]{"one", "two", "three", "four"}; |
| |
| priority_queue<string> words2{begin(wrds), end(wrds)}; |
| |
| |
| priority_queue<string> copy_words{words2}; |
| |
| |
| vector<int> values{21, 22, 12, 3, 24, 54, 56}; |
| |
| priority_queue<int> numbers{less<int>(), values}; |
| |
| |
| |
| priority_queue<int, vector<int>, greater<>> numbers2{greater<>(), values}; |
| } |
priority_queue 操作
对 priority_queue 进行操作有一些限制:
push(const T& obj)
:将 obj 的副本放到容器的适当位置,这通常会包含一个排序操作。
push(T&& obj)
:将 obj 放到容器的适当位置,这通常会包含一个排序操作。
emplace(T constructor a rgs...)
:通过调用传入参数的构造函数,在序列的适当位置构造一个T对象。为了维持优先顺序,通常需要一个排序操作。
top()
:返回优先级队列中第一个元素的引用。
pop()
:移除第一个元素。
size()
:返回队列中元素的个数。
empty()
:如果队列为空的话,返回true。
swap(priority_queue<T>& other)
:和参数的元素进行交换,所包含对象的类型必须相同。
priority_queue 也实现了赋值运算,可以将右操作数的元素赋给左操作数;同时也定义了拷贝和移动版的赋值运算符。需要注意的是,priority_queue 容器并没有定义比较运算符。因为需要保持元素的顺序,所以添加元素通常会很慢。
自定义比较函数
定义函数
| #include <vector> |
| #include <iostream> |
| #include <queue> |
| |
| using namespace std; |
| |
| 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: |
| |
| static bool cmp(ListNode *a, ListNode *b) { |
| return (*a).val > (*b).val; |
| } |
| }; |
| |
| int main() { |
| priority_queue<ListNode *, vector<ListNode *>, decltype(&Solution::cmp)> heap(Solution::cmp); |
| heap.push(new ListNode(2)); |
| heap.push(new ListNode(1)); |
| heap.push(new ListNode(6)); |
| heap.push(new ListNode(3)); |
| |
| while (!heap.empty()) { |
| cout << (*heap.top()).val << " "; |
| heap.pop(); |
| } |
| return 0; |
| } |
class 重载运算符()
| class cmp { |
| public: |
| bool operator()(int a, int b) { |
| return a > b; |
| } |
| }; |
| |
| priority_queue<int, vector<int>, cmp> heap; |
struct 重载运算符
| struct cmp { |
| bool operator()(int a, int b) { |
| return a > b; |
| } |
| }; |
| |
| priority_queue<int, vector<int>, cmp> heap; |
lambda 表达式
| auto cmp = [](int a, int b) -> bool { |
| return a > b; |
| }; |
| |
| priority_queue<int, vector<int>, decltype(cmp)> heap(cmp); |
function 包装 lambda 表达式
| function<bool(int, int)> cmp = [](int a, int b) -> bool { |
| return a > b; |
| }; |
| |
| priority_queue<int, vector<int>, decltype(cmp)> heap(cmp); |
本文作者:n1ce2cv
本文链接:https://www.cnblogs.com/sprinining/p/18370574
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步