码迷,mamicode.com
首页 > 其他好文 > 详细

容器适配器之priority_queue

时间:2015-06-06 22:07:05      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

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[] = {10, 60, 50, 20};

    std::priority_queue<int> first;
    std::priority_queue<int> second(myints, myints+4);

    typedef std::priority_queue<int, std::vector<int>, mycomparison> mypq_type;

    mypq_type third;

    third.push(10);
    third.push(50);
    third.push(60);
    third.push(20);

    std::cout<<"third contains:\n";
    while(!third.empty())
    {
        std::cout<<third.top()<< ;
        third.pop();
    }

    mypq_type fourth(myints, myints+4, mycomparison(true));

    std::cout<<"\nfourth contains:\n";
    while(!fourth.empty())
    {
        std::cout<<fourth.top()<< ;
        fourth.pop();
    }

    std::cout<<\n;

    system("pause");
    return 0;
}
/*
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(30);
    mypq.push(100);
    mypq.push(25);
    mypq.push(40);

    std::cout<<"Popping out elements...";
    while(!mypq.empty())
    {
        std::cout<< <<mypq.top();
        mypq.pop();
    } 

    std::cout<<\n;

    system("pause");
    return 0;
}

容器适配器之priority_queue

标签:

原文地址:http://www.cnblogs.com/kevinq/p/4557336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!