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)用法详解

相关推荐
似水এ᭄往昔1 分钟前
【Qt】--常用控件(显示类控件)
开发语言·qt
en.en..4 分钟前
Linux fork() 工作原理
数据结构·算法
PHP实战开发录12 分钟前
PHP文件上传成功为什么页面却找不到
开发语言·nginx·php·开发
FlightYe19 分钟前
音视频修炼之基础理论(五):AAC格式与解析
android·linux·c++·音视频·aac
地平线开发者1 小时前
bevformer算法模型详细解读
算法
liliangcsdn1 小时前
ICIR权重矩阵如何加权标准化为综合因子
算法
ShineWinsu1 小时前
对于MySQL:数据库的操作的解析
linux·数据库·c++·mysql·面试·笔试·库的操作
云小逸1 小时前
C++ 第一阶段:对象、内存与生命周期
开发语言·c++
夜航海图1 小时前
S-57 数据解剖:把一个 ENC 文件拆给你看
c++
(Charon)1 小时前
【C++】网络缓冲区设计(三):Chain Buffer链式缓冲区、动态扩容与跨节点读写
服务器·c++