【转载】STL之priority_queue
参考资料:传送门
先回顾队列的定义:
队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部。priority_queue特别之处在于,允许用户为队列中存储的元素设置优先级。这种队列不是直接将新元素放置在队列尾部,而是放在比它优先级低的元素前面。标准库默认使用<操作符来确定对象之间的优先级关系,所以如果要使用自定义对象,需要重载 < 操作符。优先队列有两种,一种是最大优先队列;一种是最小优先队列;每次取自队列的第一个元素分别是优先级最大和优先级最小的元素。
1) 优先队列的定义
包含头文件:"queue.h", "functional.h"
可以使用具有默认优先级的已有数据结构;也可以再定义优先队列的时候传入自定义的优先级比较对象;或者使用自定义对象(数据结构),但是必须重载好< 操作符。
2) 优先队列的常用操作
- q.empty() 如果队列为空,则返回true,否则返回false
- q.size() 返回队列中元素的个数
- q.pop() 删除队首元素,但不返回其值
- q.top() 返回具有最高优先级的元素值,但不删除该元素
- q.push(item) 在基于优先级的适当位置插入新元素
其中q.top()为查找操作,在最小优先队列中搜索优先权最小的元素,在最大优先队列中搜索优先权最大的元素。q.pop()为删除该元素。优先队列插入和删除元素的复杂度都是O(lgn),所以很快。
另外,在优先队列中,元素可以具有相同的优先权。
#include<iostream>
#include<functional>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std;
//定义比较结构
struct cmp1
{
bool operator ()(int &a, int &b)
{
return a > b; //最小值优先
}
};
struct cmp2
{
bool operator ()(int &a, int &b)
{
return a < b; //最大值优先
}
};
//自定义数据结构
struct number1
{
int x;
bool operator < (const number1 &a) const
{
return x > a.x; //最小值优先
}
};
struct number2
{
int x;
bool operator < (const number2 &a) const
{
return x < a.x; //最大值优先
}
};
int a[] = {14, 10, 56, 7, 83, 22, 36, 91, 3, 47, 72, 0};
number1 num1[] = {14, 10, 56, 7, 83, 22, 36, 91, 3, 47, 72, 0};
number2 num2[] = {14, 10, 56, 7, 83, 22, 36, 91, 3, 47, 72, 0};
int main()
{
priority_queue<int>que;//采用默认优先级构造队列
priority_queue<int, vector<int>, cmp1>que1; //最小值优先
priority_queue<int, vector<int>, cmp2>que2; //最大值优先
priority_queue<int, vector<int>, greater<int> >que3; //注意“>>”会被认为错误,
priority_queue<int, vector<int>, less<int> >que4; ////最大值优先
priority_queue<number1>que5; //最小优先级队列
priority_queue<number2>que6; //最大优先级队列
int i;
for(i = 0; a[i]; i++)
{
que.push(a[i]);
que1.push(a[i]);
que2.push(a[i]);
que3.push(a[i]);
que4.push(a[i]);
}
for(i = 0; num1[i].x; i++)
que5.push(num1[i]);
for(i = 0; num2[i].x; i++)
que6.push(num2[i]);
printf("采用默认优先关系:\n(priority_queue<int>que;)\n");
printf("Queue 0:\n");
while(!que.empty())
{
printf("%3d", que.top());
que.pop();
}
puts("");
puts("");
printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
printf("Queue 1:\n");
while(!que1.empty())
{
printf("%3d", que1.top());
que1.pop();
}
puts("");
printf("Queue 2:\n");
while(!que2.empty())
{
printf("%3d", que2.top());
que2.pop();
}
puts("");
puts("");
printf("采用头文件/functional/内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
printf("Queue 3:\n");
while(!que3.empty())
{
printf("%3d", que3.top());
que3.pop();
}
puts("");
printf("Queue 4:\n");
while(!que4.empty())
{
printf("%3d", que4.top());
que4.pop();
}
puts("");
puts("");
printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
printf("Queue 5:\n");
while(!que5.empty())
{
printf("%3d", que5.top());
que5.pop();
}
puts("");
printf("Queue 6:\n");
while(!que6.empty())
{
printf("%3d", que6.top());
que6.pop();
}
puts("");
return 0;
}
/*
运行结果 :
采用默认优先关系:
(priority_queue<int>que;)
Queue 0:
83 72 56 47 36 22 14 10 7 3
采用结构体自定义优先级方式一:
(priority_queue<int,vector<int>,cmp>que;)
Queue 1:
7 10 14 22 36 47 56 72 83 91
Queue 2:
83 72 56 47 36 22 14 10 7 3
采用头文件"functional"内定义优先级:
(priority_queue<int,vector<int>,greater<int>/less<int> >que;)
Queue 3:
7 10 14 22 36 47 56 72 83 91
Queue 4:
83 72 56 47 36 22 14 10 7 3
采用结构体自定义优先级方式二:
(priority_queue<number>que)
Queue 5:
7 10 14 22 36 47 56 72 83 91
Queue 6:
83 72 56 47 36 22 14 10 7 3*/
【转载】STL之priority_queue的更多相关文章
- STL之priority_queue(优先队列)
priority_queue是一个容器适配器,在这个容器里第一个数据元素是最大的.它的使用场景是什么样:如果12306抢票,为什么黄牛能抢这么多票,感觉12306那边的请求队列是一个优先队列,黄牛的请 ...
- STL 中priority_queue小结
(1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue> (2)在头文件中priority_queue定义如下: nam ...
- STL中priority_queue小结
(1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue> (2)在头文件中priority_queue定义如下: namesp ...
- C++ STL之priority_queue
STL中的priority_queue(优先队列)是一种会按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序的容器,不同优先级的情况下,top()上永远是最高优先级的数据,其底层采用的 ...
- STL之priority_queue使用简介
优先队列容器也是一种从一端入队,另一端出对的队列.不同于一般队列的是,队列中最大的元素总是位于队首位置,因此,元素的出对并非按照先进先出的要求,将最先入队的元素出对,而是将当前队列中的最大元素出对. ...
- STL之priority_queue
下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...
- 【STL】-priority_queue的用法
初始化: priority_queue<int> maxPQ; priority_queue<int,vector<int& ...
- STL之priority_queue为复合结构排序
priority_queue为复合结构排序: #include <iostream> #include <queue> using namespace std; struct ...
- C++ STL 优先队列 priority_queue 详解(转)
转自https://blog.csdn.net/c20182030/article/details/70757660,感谢大佬. 优先队列 引入 优先队列是一种特殊的队列,在学习堆排序的时候就有所了解 ...
随机推荐
- webpack 使用优化指南
前言 本文不是webpack入门文章,如果对webpack还不了解,请前往题叶的Webpack入门,或者阮老师的Webpack-Demos. 为什么要使用Webpack 与react一类模块化开发的框 ...
- SAP CRM 客户控制器与数据绑定
当用户从视图离开时,视图将失去它的数据.解决这个问题,需要引入客户控制器(Custom Controller)(译者注:SAP CRM客户端中,不同地方的Custom Controller会翻译为“客 ...
- iOS 汉字的拼音
获取汉字的拼音 #import <Foundation/Foundation.h> @interface NSString (Utils) /** * 汉字的拼音 * * @return ...
- Android中的Libraries以及Order and Export的使用。
1Add JAR 从Eclipse的现有所有工程中,添加jar包到该工程下 2Add External JARs 从Eclipse外的其他的位置,添加jar包到该工程下 3Add Variable 增 ...
- Android Stuido 常用快捷键
Android Stuido 常用快捷键 Ctrl + Z : 撤消 Ctrl + G : 定位行 Ctrl + / : 单行注释 Ctrl + Shift + Z : 恢复 Ctrl + J : 快 ...
- Java 异常处理
异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...
- SQL*Plus环境下创建PLUSTRACE角色
普通用户在SQL*Plus中开启AUTOTRACE报告时,遇到SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is ...
- loadrunner性能测试---添加windows多台压力机
添加多台压力机 1.前置条件 1)保证压力机上都安装了loadrunner Agent,并启动,状态栏中会有小卫星. 2)添加的压力机与controller所在机器是否在同一个网段,建议关 ...
- 基于pcDuino-V2的无线视频智能小车 - pcduino上的网络编程
通过获取从串口发送上来的数据 已经和上位机的连接通信和图像发送.已经对设备的控制 https://github.com/qq2216691777/pcduino_smartcar-pcduino
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...