STL之优先队列(1)
在优先队列中,优先级高的元素先出队列。
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
优先队列的第一种用法:
也是最常用的用法
priority_queue<int> qi;
通过<操作符可知在整数中元素大的优先级高。
故示例1中输出结果为:9 6 5 3 2
第二种用法:
在示例1中,如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。
priority_queue<int, vector<int>, greater<int> >qi2; //从小到大的优先级队列
可将greater改为less即为从大到小
其中 第一个参数为容器类型。 第二个参数为比较函数。
故示例2中输出结果为:2 3 5 6 9
第三种用法: 自定义优先级。
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority; //"<"为从大到小排列">"为从小打到排列
}
int priority;
int value;
};
在该结构中,value为值,priority为优先级。
通过自定义operator<操作符来比较元素中的优先级。
在示例3中输出结果为:
优先级 值
9 5
8 2
6 1
2 3
1 4
但如果结构定义如下:
struct node
{
friend bool operator> (node n1, node n2)
{
return n1.priority > n2.priority;
}
int priority;
int value;
};
则会编译不过(G++编译器)
因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。
//代码清单
#include<iostream>
#include<functional>
#include<queue>
using namespace std;
struct node
{
friend bool operator< (node n1, node n2)
{
return n1.priority < n2.priority;
}
int priority;
int value;
};
int main()
{
const int len = ;
int i;
int a[len] = {,,,,};
//示例1
priority_queue<int> qi;
for(i = ; i < len; i++)
qi.push(a[i]);
for(i = ; i < len; i++)
{
cout<<qi.top()<<" ";
qi.pop();
}
cout<<endl;
//示例2
priority_queue<int, vector<int>, greater<int> >qi2;
for(i = ; i < len; i++)
qi2.push(a[i]);
for(i = ; i < len; i++)
{
cout<<qi2.top()<<" ";
qi2.pop();
}
cout<<endl;
//示例3
priority_queue<node> qn;
node b[len];
b[].priority = ; b[].value = ;
b[].priority = ; b[].value = ;
b[].priority = ; b[].value = ;
b[].priority = ; b[].value = ;
b[].priority = ; b[].value = ; for(i = ; i < len; i++)
qn.push(b[i]);
cout<<"优先级"<<'\t'<<"值"<<endl;
for(i = ; i < len; i++)
{
cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
qn.pop();
}
return ;
}
STL之优先队列(1)的更多相关文章
- STL之优先队列
STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素 ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- STL中优先队列的使用
普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有最高级先出的行为特征.我们来说一下C++的 ...
- STL priority_queue 优先队列 小记
今天做题发现一个很有趣的地方,竟然还是头一次发现,唉,还是太菜了. 做图论用STL里的priority_queue去优化prim,由于特殊需求,我需要记录生成树中是用的哪些边. 于是,我定义的优先队列 ...
- STL之优先队列(priority_queue)
转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...
- W - stl 的 优先队列 Ⅲ
Description In a speech contest, when a contestant finishes his speech, the judges will then grade h ...
- V - stl 的 优先队列 Ⅱ
Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every morni ...
- hdu 4393 Throw nails(STL之优先队列)
Problem Description The annual school bicycle contest started. ZL is a student in this school. He is ...
- hdu1716排列2(stl:next_permutation+优先队列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
随机推荐
- 【BZOJ3223】 Tyvj 1729 文艺平衡树 Splay
Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 ...
- BZOJ4513: [Sdoi2016]储能表
Description 有一个 n 行 m 列的表格,行从 0 到 n−1 编号,列从 0 到 m−1 编号.每个格子都储存着能量.最初,第 i 行第 j 列的格子储存着 (i xor j) 点能量. ...
- 创建和使用动态链接库 (C++)
创建和使用动态链接库 (C++) 转载:http://msdn.microsoft.com/zh-cn/library/ms235636.aspx 此分步演练演示如何创建用于 C++ 应用的动态链接库 ...
- IE6及以上版本fixed问题解决方案,页面右下角固定页面,可以最大化、最小化、正规显示
在窗口固定位置显示内容使用fixed,但是 IE 6 不支持,后来我搜了很多方法,都没有作用,后来类比着一个网站的代码,使用absolute .z-index解决了问题. 页面div结构: <d ...
- Struts 2入门案例及登录
一:入门案例 步骤如下: 1.导入jar包 2.配置web.xml文件 3.在src下创建名称为struts.xml的配置文件 4.创建编写HelloWorldAction 5.创建index.jsp ...
- MVC VS2012 Code First 数据库迁移教程
1.在“服务资源管理器”连接数据库 2.打开工具-Nuget程序包管理器“程序包管理器控制台” 3.控制台输入命令:PM> Enable-Migrations -StartUpProjectNa ...
- HDU2955 背包DP
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 树莓派3b+ 用samba与windows共享文件
1. 树莓派安装samba sudo apt-get install samba 2. 设置一个公共目录 cd /;sudo mkdir share;sudo chmod 777 sharesudo ...
- 手动创建oem
[oracle@std bin]$ /u02/app/product//db_1/bin/emca -config dbcontrol db -repos create STARTED EMCA at ...
- MyEclipse修改项目名称后,部署到tomcat问题
问题描述: 修改项目名称后,部署到tomcat server,部署出来的文件夹名还是旧的名称. 解决方案: 光把项目重命名是不够的,还要修改一下Myeclipse里面的配置. 工程名->右键-& ...