C++优先队列用法案例详解

作者:华山青竹 时间:2022-10-23 17:35:01 

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

普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。

在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (first in, largest out)的行为特征。

首先要包含头文件#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、基本类型优先队列的例子:


#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;
}

运行结果:


4 3 2 1 0
0 1 2 3 4
cbd abcd abc
请按任意键继续. . .

2、用pair做优先队列元素的例子:

规则:pair的比较,先比较第一个元素,第一个相等比较第二个。


#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();
   }
}

运行结果:


2 5
1 3
1 2
请按任意键继续. . .

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(b);
   f.push(c);
   f.push(a);
   while (!f.empty())
   {
       cout << f.top().x << '\n';
       f.pop();
   }
}

运行结果:


3
2
1

3
2
1
请按任意键继续. . .

来源:https://www.cnblogs.com/huashanqingzhu/p/11040390.html

标签:C++,优先队列
0
投稿

猜你喜欢

  • 详解Mybatis极其(最)简(好)单(用)的一个分页插件

    2021-09-25 03:00:35
  • Java正则之贪婪匹配、惰性匹配

    2022-08-14 16:41:23
  • Jenkins任务批量修改的技巧分享

    2023-01-11 09:47:44
  • 一行命令同时修改maven项目中多个module的版本号的方法

    2023-09-13 09:43:10
  • Spring Boot 静态资源处理方式

    2022-09-14 11:14:39
  • Android用过TextView实现跑马灯效果的示例

    2023-07-27 11:16:54
  • Android编程实现系统重启与关机的方法

    2022-01-09 20:56:49
  • C#简单读写txt文件的方法

    2023-01-17 06:30:27
  • 如何通过JVM角度谈谈Java的clone操作

    2023-10-13 09:54:40
  • Java实现在线聊天功能

    2021-10-18 22:16:23
  • Java 梳理总结关于static关键字常见问题

    2021-12-11 11:49:01
  • Android定时器Timer的停止和重启实现代码

    2022-10-03 23:25:43
  • 利用Spring Boot操作MongoDB的方法教程

    2023-11-29 11:14:27
  • Java 多线程并发编程提高数据处理效率的详细过程

    2021-06-29 04:19:39
  • Android ViewPager实现页面左右切换效果

    2022-12-28 04:49:35
  • Bootstrap 下拉菜单.dropdown的具体使用方法

    2023-07-08 19:10:46
  • java 并发线程个数的如何确定

    2022-01-01 21:52:13
  • Mybatis逆工程jar包的修改和打包

    2023-06-03 09:28:14
  • Spring的自动装配Bean的三种方式

    2023-08-24 23:05:15
  • My eclipse 端口占用(9360)问题解决办法

    2023-05-19 02:34:32
  • asp之家 软件编程 m.aspxhome.com