C++中的优先级队列priority_queue和双端队列deque用法详解

C++中的优先级队列priority_queue和双端队列deque用法详解

文章目录

1. priority_queue

使用优先级队列需要包含头文件#include <queue>,优先级队列priority_queue的使用和普通的队列非常类似,只不过优先级队列的底层实现是Heap能够对队列中的元素进行排序,我们可以让大的元素排在前面或者让小的元素排在前面

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

优先级队列的定义:priority_queue<Type, Container, Functional>

  • Type 指定数据类型
  • Container 指定容器,一般默认是vector
  • Functional 需要包含头文件#include <functional>,可以使用std::greater或者std::less来指定比较的方式,如果使用std::greater就指定了小顶堆,队首保留最小的元素(升序排列),如果使用std::less就指定了大顶堆,队首保留最大的元素(降序排列)
cpp 复制代码
#include <queue>
#include <vector>
#include <functional>
// 定义一个小顶堆minHeap
std::priority_queue <int, std::vector<int>, std::greater<int>> minHeap;
// 定义一个大顶堆maxHeap
std::priority_queue <int, std::vector<int>, std::less<int>> maxHeap;

1.1 一个基本的示例:

cpp 复制代码
#include <iostream>
#include <queue>
#include <functional>

int main() {
    // 小顶堆,队首是最小元素
    std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap;
    minHeap.push(3);
    minHeap.push(1);
    minHeap.push(2);

    std::cout << "Top of minHeap: " << minHeap.top() << std::endl;  // 输出 1

    // 大顶堆,队首是最大元素
    std::priority_queue<int> maxHeap;
    maxHeap.push(3);
    maxHeap.push(1);
    maxHeap.push(2);

    std::cout << "Top of maxHeap: " << maxHeap.top() << std::endl;  // 输出 3

    return 0;
}

输出

shell 复制代码
Top of minHeap: 1
Top of maxHeap: 3

1.2 使用pair的示例

cpp 复制代码
#include <iostream>
#include <queue>
#include <vector>
#include <functional>

int main() {
    // 定义一个小顶堆,基于 pair 的第一个元素进行排序
    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> minHeap;
    // 插入一些 pair
    minHeap.push({3, 100});
    minHeap.push({1, 200});
    minHeap.push({2, 300});
    // 获取并输出堆顶元素
    std::pair<int, int> top = minHeap.top();
    std::cout << "Top of minHeap: (" << top.first << ", " << top.second << ")" << std::endl;  // 输出 (1, 200)
    // 弹出堆顶元素
    minHeap.pop();
    // 再次获取并输出堆顶元素
    top = minHeap.top();
    std::cout << "Top of minHeap after pop: (" << top.first << ", " << top.second << ")" << std::endl;  // 输出 (2, 300)
    return 0;
}

输出

shell 复制代码
Top of minHeap: (1, 200)
Top of minHeap after pop: (2, 300)

2. deque

deque 是双端队列(Double-Ended Queue),支持在两端进行高效的插入和删除操作。与 std::vector 类似,它也提供了随机访问功能,但与 vector 不同的是,deque 可以在头部和尾部都进行高效的插入和删除。

  • push_back():在队列尾部添加元素。
  • push_front():在队列头部添加元素。
  • pop_back():移除队列尾部的元素。
  • pop_front():移除队列头部的元素。
  • front():访问队列头部的元素。
  • back():访问队列尾部的元素。
  • at(index):访问指定位置的元素。
  • operator[]:随机访问指定位置的元素。
  • size():返回队列中的元素个数。
  • empty():检查队列是否为空。
  • clear():移除所有元素,使 deque 为空。
  • insert(pos, value):在指定位置插入元素。
  • erase(pos):移除指定位置的元素。
  • resize():调整 deque 的大小。

一个使用示例:

cpp 复制代码
#include <iostream>
#include <deque>

int main() {
    // 创建一个空的 deque 容器
    std::deque<int> dq;
    // 在队列尾部添加元素
    dq.push_back(10);
    dq.push_back(20);
    dq.push_back(30);
    // 在队列头部添加元素
    dq.push_front(5);
    dq.push_front(1);
    // 输出 deque 中的所有元素
    std::cout << "Elements in deque: ";
    for(int i : dq) {
        std::cout << i << " ";  // 输出 1 5 10 20 30
    }
    std::cout << std::endl;
    // 访问头部和尾部的元素
    std::cout << "Front element: " << dq.front() << std::endl;  // 输出 1
    std::cout << "Back element: " << dq.back() << std::endl;    // 输出 30
    // 使用 `at()` 方法和 `operator[]` 访问元素
    std::cout << "Element at index 2: " << dq.at(2) << std::endl;  // 输出 10
    std::cout << "Element at index 3: " << dq[3] << std::endl;     // 输出 20
    // 移除队列头部和尾部的元素
    dq.pop_front();
    dq.pop_back();
    // 输出移除后的元素
    std::cout << "Elements after pop: ";
    for(int i : dq) {
        std::cout << i << " ";  // 输出 5 10 20
    }
    std::cout << std::endl;
    // 检查大小和是否为空
    std::cout << "Size of deque: " << dq.size() << std::endl;  // 输出 3
    std::cout << "Is deque empty? " << (dq.empty() ? "Yes" : "No") << std::endl;  // 输出 No
    // 清空 deque
    dq.clear();
    std::cout << "Deque cleared. Is deque empty? " << (dq.empty() ? "Yes" : "No") << std::endl;  // 输出 Yes
    return 0;
}

输出

shell 复制代码
Elements in deque: 1 5 10 20 30
Front element: 1
Back element: 30
Element at index 2: 10
Element at index 3: 20
Elements after pop: 5 10 20
Size of deque: 3
Is deque empty? No
Deque cleared. Is deque empty? Yes

Reference

Microsoft C++ queue
std::priority_queue
c++优先队列(priority_queue)用法详解

相关推荐
杯莫停丶2 小时前
设计模式之:模板模式
开发语言·设计模式
开发者小天2 小时前
调整为 dart-sass 支持的语法,将深度选择器/deep/调整为::v-deep
开发语言·前端·javascript·vue.js·uni-app·sass·1024程序员节
老猿讲编程3 小时前
C++中的奇异递归模板模式CRTP
开发语言·c++
.格子衫.4 小时前
022数据结构之树状数组——算法备赛
数据结构·算法·1024程序员节
黑科技Python5 小时前
生活中的“小智慧”——认识算法
学习·算法·生活
Yupureki5 小时前
从零开始的C++学习生活 16:C++11新特性全解析
c语言·数据结构·c++·学习·visual studio
紫荆鱼5 小时前
设计模式-迭代器模式(Iterator)
c++·后端·设计模式·迭代器模式
汤姆yu5 小时前
基于python的化妆品销售分析系统
开发语言·python·化妆品销售分析
ScilogyHunter6 小时前
C语言标准库完全指南
c语言·开发语言
sali-tec6 小时前
C# 基于halcon的视觉工作流-章52-生成标定板
开发语言·图像处理·人工智能·算法·计算机视觉