template <class T, class Container = vector<T>,
                class Compare = less<typename Container::value_type> > class priority_queue;

Priority queue
Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion.
[优先队列(priority queue)是一种容器适配器,它会根据严格弱排序将优先级最高的元素移动到队首]
This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue).
[优先队列的这种上下文就像是堆,元素可以在任何时刻插入到堆中,并且只能读取最大堆元素(即位于优先队列顶端的元素)]
Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.
[容器适配器是一个类,这个类使用一个特定的容器类对象作为它的内在容器,该内在容器提供了一系列的成员函数来读取它的元素。元素从内在容器的尾部弹出,即是从优先队列的顶端弹出]
The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall be accessible through random access iterators and support the following operations:
[优先队列的内在容器可以是一个标准容器类模板或者其他专门设计的容器类,但无论如何,内在容器都应该支持随机存储迭代器以及以下几种操作:]
    empty()
    size()
    front()
    push_back()
    pop_back()
The standard container classes vector and deque fulfill these requirements. By default, if no container class is specified for a particular priority_queue class instantiation, the standard container vector is used.
[标准容器vector和deque满足这些要求。默认的内在容器是vector]
Support of random access iterators is required to keep a heap structure internally at all times. This is done automatically by the container adaptor by automatically calling the algorithm functions make_heap, push_heap and pop_heap when needed.
[支持随机存储迭代器要求优先队列内部必须时刻保持一个堆结构,这个操作由容器适配器通过自动调用算法函数make_heap、push_heap即pop_heap来自动完成]

堆数据结构是一种数组对象,它可以被视为一颗完全二叉树结构。它的特点是父节点的值大于(小于)两个子节点的值(分别称为大顶堆和小顶堆)。它常用于管理算法执行过程中的信息,应用场景包括堆排序,优先队列等。

/*
//construct priority_queue
priority_queue (const Compare& comp = Compare(), const Container& ctnr = Container());
priority_queue (InputIterator first, InputIterator last, const Compare& comp = Compare(), const Container& ctnr = Container()); A priority_queue keeps internally a comparing function and a container object as data, which are copies of comp and ctnr respectively.
[优先队列会将一个比较函数和一个容器对象数据,它们是各自传递到构造函数中的参数comp和ctnr]
The range version (2), on top that, inserts the elements between first and last (before the container is converted into a heap).
[第二种方式会将[first, second)之间的元素插入到优先队列中,然后转换成堆(通过make_heap排序)] comp
Comparison object to be used to order the heap.
[comp是用于对堆进行排序的比较对象]
This may be a function pointer or function object able to perform a strict weak ordering by comparing its two arguments.
[comp可以是一个能够进行严格弱排序的函数指针或者函数对象,且有两个参数]
Compare is the third class template paramete ( by default: less<T>).
[comp的数据类型是优先队列的第三个模板参数,默认情况下为less<T>] ctnr
Container object.
[ctnr是一个容器类对象]
Container is the second class template parameter (the type of the underlying container for the priority_queue; by default: vector<T>).
[ctnr的数据类型是优先队列的第二个模板参数,即优先队列的内在容器的类型,默认情况下为vector<T>]
*/ #include <iostream>
#include <queue>
#include <vector>
#include <functional> class mycomparison
{
bool reverse;
public:
mycomparison(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()
{
int myints[] = {, , , }; std::priority_queue<int> first;
std::priority_queue<int> second(myints, myints+); typedef std::priority_queue<int, std::vector<int>, mycomparison> mypq_type; mypq_type third; third.push();
third.push();
third.push();
third.push(); std::cout<<"third contains:\n";
while(!third.empty())
{
std::cout<<third.top()<<' ';
third.pop();
} mypq_type fourth(myints, myints+, mycomparison(true)); std::cout<<"\nfourth contains:\n";
while(!fourth.empty())
{
std::cout<<fourth.top()<<' ';
fourth.pop();
} std::cout<<'\n'; system("pause");
return ;
}
/*
bool empty() const;
size_type size() const;
void push(const value_type& val);
void pop();
const value_type& top() const;
*/ #include <iostream>
#include <queue> int main()
{
std::priority_queue<int> mypq; mypq.push();
mypq.push();
mypq.push();
mypq.push(); std::cout<<"Popping out elements...";
while(!mypq.empty())
{
std::cout<<' '<<mypq.top();
mypq.pop();
} std::cout<<'\n'; system("pause");
return ;
}

容器适配器之priority_queue的更多相关文章

  1. 容器适配器之stack

    参见http://www.cplusplus.com/reference/stack/stack/ template<class T, class Container = deque<T& ...

  2. 容器适配器之queue

    转载http://blog.csdn.net/thefutureisour/article/details/7751846容器适配器容器适配器其实就是一个接口转换装置,使得我们能用特定的方法去操作一些 ...

  3. C++STL模板库适配器之优先级队列

    目录 适配器之优先级队列 一丶优先级队列简介(priority_queue) 二丶优先级队列代码演示 1.优先级队列代码以及使用简介 适配器之优先级队列 一丶优先级队列简介(priority_queu ...

  4. C++STL模板库适配器之queue队列

    目录 适配器之队列 一丶队列简介 二丶队列(queue)代码操作 1.常用方法 适配器之队列 一丶队列简介 队列是先进先出的数据结构. 在STL中使用 queue表示. 底层使用的是序列容器deque ...

  5. C++STL模板库适配器之stack容器

    目录 适配器 一丶适配器简介 二丶栈(stack)用法 1.栈的常用方法 适配器 一丶适配器简介 Stl中的适配器,有栈 (stack) 队列 queue 根priority_queue 适配器都是包 ...

  6. Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)

    摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...

  7. 函数对象适配器之ptr_fun的使用示例

    //============================================================================ // Name : CopyInts4.c ...

  8. [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段

      收藏ArrayAdapter.SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法. 1.ArrayAdapter 只可以简单的显示一行文本 代码片段: A ...

  9. 适配器之SimpleAdapter

    前言: 在写适配器时,SimpleAdapter会经常使用到,虽然他比ArrayAdapter复杂,但是也提供了更多的功能 正文: 我们接下来先从SimpleAdapter中较为简单的显示两行文本开始 ...

随机推荐

  1. (笔记)angular 路由

  2. 设计模式-观察者模式(Observer)

    简介: 观察者模式,也称为订阅-发布模式,定义对象间的一种一对多的依赖关系,当一个对象的状态发生变化时,所有依赖他的对象都得到通知并被自动更新. 主要由以下几个部分组成: a.Subject目标对象. ...

  3. Ninject在mvc中的简单配置

    前言 Ninject是一款开源的轻量级的依赖注入插件.从接触ioc以来,一直都是使用这个,感觉用起来还是不错的,配置起来也很方便简单.在mvc中更是基本傻瓜式的配置. 开发前的准备 新建一个mvc3项 ...

  4. Knockout.Js官网学习(style绑定、attr绑定)

    Style绑定 style绑定是添加或删除一个或多个DOM元素上的style值.比如当数字变成负数时高亮显示,或者根据数字显示对应宽度的Bar.(注:如果你不是应用style值而是应用CSS clas ...

  5. ip的正则表达式 完美版

    IP地址的长度为32位2进制,分为4段,每段8位,用十进制数字表示,每段数字范围为0~255,段与段之间用英文句点“.”隔开.例如:IP地址为10.0.0.100. 分析IP地址的每组数特点:百位,十 ...

  6. c语言学习第四天数据类型1

    int   代表整数,它在内存中占4个字节,二进制的表示方式是占用了三十二位,二进制中只包含0和1,那它的最大值就是全为1,但int是 有符号类型,所以最高位(左边的第一位)要拿出来做符号位,这样就只 ...

  7. php 解决json_encode中文问题

    众所周知使用json_encode可以方便快捷地将对象进行json编码,但是如果对象的属性中存在着中文,问题也就随之而来了.json_encode会将中文转换为unicode编码例如:'胥'经过jso ...

  8. 用js读、写、删除Cookie

    //已经验证过 // JavaScript Document //使用说明:  //设置缓存:setCookie("name",value); //获取缓存:var name=ge ...

  9. 【MySql】5.6.14版本的安装和测试

    当前状态:apache2.4.6和php5.5.6已经安装成功: mysql的安装和测试: 一.安装mysql5.6.14,参考http://wenku.baidu.com/link?url=_0jk ...

  10. C#的编译

    Windows上的编译 1:先将C:\Windows\Microsoft.NET\Framework\v3.5配置到系统环境变量的path里. 2:写C#代码 demo1.txt using Syst ...