STL学习之priority_queue适配器
2012-06-30 13:45
274 查看
priority_queue
优先队列是容器适配器类型,根据某些严格的弱排序条件,专门设计了第一个元素总最大元素的容器。很像堆,可以检索最大的堆元素(在优先队列中的最顶的元素)并且可以无限制的插入元素。
优先队列作为容器适配器, 用一个具体的容器类的封装对象作为其底层容器,提供一个具体的访问容器元素的成员函数集合。元素都是从具体的容器的“尾部”进行弹出(pop)操作,这个“尾部”就是所谓的优先队列的顶。
底层容器可以是任何的标准容器类模板或者其他的具体的设计容器类。仅有的要求是需要支持下面的操作:
front()
push_back()
pop_back()
因此可以使用vector,deque这样标准容器类模板。默认的,如果对一个特殊的priority_queue类没有指定容器类,使用vector标准类模板。
为了时刻保持堆的内部结构,需要支持迭代器的随机访问。当适当的时候通过调用make_heap,push_heap和pop_heap算法由容器适配器自动执行。
在c++标准的模板类的定义中,优先队列是有三个模板参赛的模板:
template< class T. class Cotainer = vector<T>, class Compare = less<typename Container::value_type> > class priority_queue;参数:
T:元素类型。
Container:用于存储和访问元素的底层容器对象类型。
Compare:Comparison 类:com(a,b)类,comp是此类的对象并且a和b是容器的元素,如果a在b之前放入容器,返回true。这个可以是一个类执行函数调用操作或者指向函数的指针。默认为less<T>,less<T>的返回和小于(a<b)操作类似。priority_queue对象当元素插入或者删除的时候使用此表达式去确保在优先队列中弹出的元素始终是最大的元素。
priority_queue::priority_queue
explicit priority_queue( const Compare& x = Compare(), const Container& y = Container() ); template< class InputIterator> priority_queue( InputIterator first, InputIterator last, const Compare& x = Compare(), const Container& y = Container() );构造一个priority_queue容器适配器对象。
容器适配器把容器对象当作数据。如果有的话,容器对象是参数y的一个拷贝,否则是一个空的容器。
priority_queue满足堆优先(弹出的元素总是容器中的最大的元素)操作,但是确切的序列准则可能会因合适的y参数被修改。
(Because priority_queues satisfy the heap property the element popped from it is always the highest in the container. But the exact ordering criterium
to determine this may be any that follow a strict weak ordering, which may be modified with an appropiatey parameter.
)。
参数
x
被用于严格的若排序的Comparision 对象。
y
容器对象。
容器是第二个类模板参数(为priority_queue提供的底层容器类型);默认情况下为vector<T>。
first,last
在一个序列中的第一个和最后一个位置的输入迭代器,采用[first,last)区间。
举例:
#include <iostream> #include <queue> using namespace std; class MyCmp { bool reverse; public: MyCmp(const bool& revParam=false) { reverse = revParam; } bool operator() (const int& lhs, const int& rhs) const { if (reverse) return (lhs < rhs); else return (lhs < rhs); } }; int main(void) { int myInts[] = {10,20,60,15}; priority_queue<int> first; priority_queue<int> second(myInts, myInts+4); priority_queue<int, vector<int>, greater<int> > third(myInts, myInts+4); priority_queue<int, vector<int>, MyCmp> fourth; typedef priority_queue<int, vector<int>, MyCmp> mypq_type; mypq_type fifth(MyCmp()); mypq_type sixth(MyCmp(true)); return 0; }无执行结果。
priority_queue::top
const value_type& top() const;
返回priority_queue的最“顶”元素的一个常引用。这个最“顶”元素是priority_queue中比较高(compares higher),并且当调用priority_queue:pop调用时弹出的元素。
这个成员函数实际调用底部容器对象的fron成员函数。
举例:
#include <iostream> #include <queue> using namespace std; int main(void) { priority_queue<int> myqu; myqu.push(10); myqu.push(20); myqu.push(15); while(!myqu.empty()) { cout << "myqu.top() is now "<< myqu.top() << endl; myqu.pop(); } return 0; }执行结果:
liujl@liujl-Rev-1-0:~/mycode/STL$./queue_priority_top myqu.top() is now 20 myqu.top() is now 15 myqu.top() is now 10从程序中也可以看出,执行pop的时候,也是按照排序的顺序弹出的。
其他成员函数(和queue类似,不再赘述):
empty
测试容器是否为空。
size返回容器的大小。
push插入元素。
pop弹出元素。
相关文章推荐
- STL学习之priority_queue适配器
- STL学习之priority_queue适配器
- STL: priority_queue学习
- STL学习笔记-- priority_queue
- 标准模板库(STL)学习指南之priority_queue优先队列
- STL之容器适配器priority_queue的实现框架
- stl 顺序容器适配器之stack, priority_queue, queue用法
- STL 笔记(三) 容器适配器 stack、queue、priority_queue
- C++的STL容器之容器适配器:stack、queue、priority_queue
- C++ STL 学习笔记 priority_queue
- STL之容器适配器:stack,queue , priority_queue
- stl 顺序容器适配器之stack, priority_queue, queue用法
- STL学习之queue适配器
- <STL学习笔记>Priority_queue
- STL 笔记(三) 容器适配器 stack、queue、priority_queue
- STL学习之八:priority_queue用法示例
- STL学习笔记— —特殊容器priority_queue
- 从零开始学C++之STL(十一):容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例
- STL学习笔记6 -- 栈stack 、队列queue 和优先级priority_queue 三者比较
- 从一道整数合并问题学习 STL 之make_heap &&priority_queue&&multiset