priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法
实现,也算是堆的另外一种形式。
先写一个用 STL 里面堆算法实现的与真正的STL里面的 priority_queue 用法相
似的 priority_queue, 以加深对 priority_queue 的理解
#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
classpriority_queue
{
private:
vector<int>da<wbr style="line-height:23px">ta;<br style="line-height:23px"><img alt="priority_queue用法(转载) - 小六 - 小六的博客" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align="top" style="line-height:23px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%"><br style="line-height:23px"><img alt="priority_queue用法(转载) - 小六 - 小六的博客" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align="top" style="line-height:23px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%"></wbr>public:
voidpush(intt){
da<wbr style="line-height:23px">ta.push_back(t);<br style="line-height:23px"><img alt="priority_queue用法(转载) - 小六 - 小六的博客" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align="top" style="line-height:23px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%">push_heap(da<wbr style="line-height:23px">ta.begin(),da<wbr style="line-height:23px">ta.end());<br style="line-height:23px"><img alt="priority_queue用法(转载) - 小六 - 小六的博客" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" style="line-height:23px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%">}</wbr></wbr></wbr>
voidpop(){
pop_heap(da<wbr style="line-height:23px">ta.begin(),da<wbr style="line-height:23px">ta.end());<br style="line-height:23px"><img alt="priority_queue用法(转载) - 小六 - 小六的博客" src="http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif" align="top" style="line-height:23px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%">da<wbr style="line-height:23px">ta.pop_back();<br style="line-height:23px"><img alt="priority_queue用法(转载) - 小六 - 小六的博客" src="http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" style="line-height:23px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; max-width:100%">}</wbr></wbr></wbr>
inttop(){returnda<wbr style="line-height:23px">ta.front();}</wbr>
intsize(){returnda<wbr style="line-height:23px">ta.size();}</wbr>
boolempty(){returnda<wbr style="line-height:23px">ta.empty();}</wbr>
};
intmain()
{
priority_queuetest;
test.push(3);
test.push(5);
test.push(2);
test.push(4);
while(!test.empty()){
cout<<test.top()<<endl;
test.pop();}
return0;
}
STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。
priority_queue 对于基本类型的使用方法相对简单。
他的模板声明带有三个参数,priority_queue<Type, Container, Functional>
Type 为数据类型, Container 为保存数据的容器,Functional 为元素比较方式。
Container 必须是用数组实现的容器,比如 vector, deque 但不能用 list.
STL里面默认用的是 vector. 比较方式默认用 operator< , 所以如果你把后面俩个
参数缺省的话,优先队列就是大顶堆,队头元素最大。
看例子
#include<iostream>
#include<queue>
usingnamespacestd;
intmain(){
priority_queue<int>q;
for(inti=0;i<10;++i)q.push(rand());
while(!q.empty()){
cout<<q.top()<<endl;
q.pop();
}
getchar();
return0;
}
如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
例子:
#include<iostream>
#include<queue>
usingnamespacestd;
intmain(){
priority_queue<int,vector<int>,greater<int>>q;
for(inti=0;i<10;++i)q.push(rand());
while(!q.empty()){
cout<<q.top()<<endl;
q.pop();
}
getchar();
return0;
}
对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
先看看例子:
#include<iostream>
#include<queue>
usingnamespacestd;
structNode{
intx,y;
Node(inta=0,intb=0):
x(a),y(b){}
};
booloperator<(Nodea,Nodeb){
if(a.x==b.x)returna.y>b.y;
returna.x>b.x;
}
intmain(){
priority_queue<Node>q;
for(inti=0;i<10;++i)
q.push(Node(rand(),rand()));
while(!q.empty()){
cout<<q.top().x<<''<<q.top().y<<endl;
q.pop();
}
getchar();
return0;
}
自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明
priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 没有定义,如果想用这种方法定义
则可以按如下方式
例子:
#include<iostream>
#include<queue>
usingnamespacestd;
structNode{
intx,y;
Node(inta=0,intb=0):
x(a),y(b){}
};
structcmp{
booloperator()(Nodea,Nodeb){
if(a.x==b.x)returna.y>b.y;
returna.x>b.x;}
};
intmain(){
priority_queue<Node,vector<Node>,cmp>q;
for(inti=0;i<10;++i)
q.push(Node(rand(),rand()));
while(!q.empty()){
cout<<q.top().x<<''<<q.top().y<<endl;
q.pop();
}
getchar();
return0;
}
priority_queue
在优先队列中,优先级高的元素先出队列。
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
优先队列的第一种用法,也是最常用的用法:
通过<操作符可知在整数中元素大的优先级高。
故示例1中输出结果为:9 6 5 3 2
第二种方法:
在示例1中,如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。
其中
第二个参数为容器类型。
第二个参数为比较函数。
故示例2中输出结果为:2 3 5 6 9
第三种方法:
自定义优先级。
{
friendbooloperator<(noden1,noden2)
{
returnn1.priority<n2.priority;
}
intpriority;
intvalue;
};
在该结构中,value为值,priority为优先级。
通过自定义operator<操作符来比较元素中的优先级。
在示例3中输出结果为:
优先级 值
9 5
8 2
61
23
14
但如果结构定义如下:
{
friendbooloperator>(noden1,noden2)
{
returnn1.priority>n2.priority;
}
intpriority;
intvalue;
};
则会编译不过(G++编译器)
因为标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
而且自定义类型的<操作符与>操作符并无直接联系,故会编译不过。
//代码清单
#include<iostream>
#include<functional>
#include<queue>
usingnamespacestd;
structnode
{
friendbooloperator<(noden1,noden2)
{
returnn1.priority<n2.priority;
}
intpriority;
intvalue;
};
intmain()
{
constintlen=5;
inti;
inta[len]={3,5,9,6,2};
//示例1
priority_queue<int>qi;
for(i=0;i<len;i++)
qi.push(a[i]);
for(i=0;i<len;i++)
{
cout<<qi.top()<<"";
qi.pop();
}
cout<<endl;
//示例2
priority_queue<int,vector<int>,greater<int>>qi2;
for(i=0;i<len;i++)
qi2.push(a[i]);
for(i=0;i<len;i++)
{
cout<<qi2.top()<<"";
qi2.pop();
}
cout<<endl;
//示例3
priority_queue<node>qn;
nodeb[len];
b[0].priority=6;b[0].value=1;
b[1].priority=9;b[1].value=5;
b[2].priority=2;b[2].value=3;
b[3].priority=8;b[3].value=2;
b[4].priority=1;b[4].value=4;
for(i=0;i<len;i++)
qn.push(b[i]);
cout<<"优先级"<<'\t'<<"值"<<endl;
for(i=0;i<len;i++)
{
cout<<qn.top().priority<<'\t'<<qn.top().value<<endl;
qn.pop();
}
return0;
}
相关推荐
priority_queue用法,希望对大家会有所帮助
主要介绍了 STL priority_queue(优先队列)详解的相关资料,需要的朋友可以参考下
priority_queue 源码
priority_queue_learn.cpp queue_learn.cpp set_learn.cpp stack_learn.cpp static_var_in_class.cpp std_except.cpp std_io.cpp stl_alg_learn.cpp string_learn.cpp test_init.cpp type_change.cpp vector_learn....
压缩包中包括STL数据文件的读取类,包括ASCII格式和Binary格式。
使用priority_queue(即堆)的不同方式 :mount_fuji: 默认声明 priority_queue< int > pq; // creates max-heap priority_queue< int , vector< int >> pq; // creates max-heap 为 priority_queue 编写...
implement huffman algorithm with stl priority-queue, first you must have the file, then the result is saved
c++ std stl各容器的应用场合及性能 map hash_map unordered_map multimap list forward_list vector set hash_set multiset unsorted_set queue deque priority_queue
这个文档介绍了PBDS库中的一些数据结构,包括rope、priority_queue以及tree的用法。
gdb 打印功能扩展 ...# std::priority_queue<T> -- via ppqueue command # std::bitset<n> -- via pbitset command # std::string -- via pstring command # std::widestring -- via pwstring command
本人做的一个二叉堆的课件,附带STL中的priority_queue
STL-ARR,arry, file,list,map,priority_queue,set,share_ptr,stack,string,template,等基本数据类型
1、STL标准容器类简介 标准容器类 说明 顺序性容器 vector 相当与数组,从后面快速的插入与删除,直接访问任何元素 deque 双队列,从前面或后面快速的插入与删除,... priority_queue 最高优先级元素总是第一个出列
包含了STL中 dequeue,list,map,multimap,multiset,priority_queue,queue,set,stack,vector等10个代码例子,并列举了各个容器对应的全部函数使用方式,以及函数的调用方式与代码注释,能使您快速掌握STL的...
第20章 priority_queue优先队列容器 275 20.1 priority_queue技术原理 275 20.2 priority_queue应用基础 278 20.3 本章小结 281 第四篇 C++ STL算法技术 第21章 非变易算法 284 21.1 逐个容器元素for_...
该篇分为十一部分,分别是:vector类的主要成员...stack类的主要成员、queue类的主要成员、priority_queue类的组要成员、set类的主要成员、multiset类的主要成员、map类的主要成员、multimap类的主要成员、STL算法函数
第20章 priority_queue优先队列容器 275 20.1 priority_queue技术原理 275 20.2 priority_queue应用基础 278 20.3 本章小结 281 第四篇 C++ STL算法技术 第21章 非变易算法 284 21.1 逐个容器元素for_...
讲STL的栈,队列,优先队列,内有代码清单使用方法等等
第20章 priority_queue优先队列容器 275 20.1 priority_queue技术原理 275 20.2 priority_queue应用基础 278 20.3 本章小结 281 第四篇 C++ STL算法技术 第21章 非变易算法 284 21.1 逐个容器元素for_...
MyStl 实现自己的STL 环境 Microsoft Windows 10 Visual Studio 2015 c++11 要点 模板实现 traits编程技巧 c++11 自定义内存管理 ...顺序容器适配器:stack,queue ...容器适配器:priority_queue 迭代器:反向迭代器