c++中STL之heap, priority_queue使用

一、heap

heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制。而这个实现机制中的max-heap实际上是以一个vector表现的完全二叉树(complete binary tree)。STL在中实现了对存储在vector/deque 中的元素进行堆操作的函数,包括make_heap, pop_heap, push_heap, sort_heap,对不愿自己写数据结构堆的C++选手来说,这几个算法函数很有用,详细解释可以参见: http://www.cplusplus.com/reference/algorithm/make_heap/

下面的_First与_Last为可以随机访问的迭代器(指针),_Comp为比较函数(仿函数),其规则——如果函数的第一个参数小于第二个参数应返回true,否则返回false。

1.make_heap():

make_heap(_First, _Last)

make_heap(_First, _Last, _Comp)

默认是建立最大堆的。对int类型,可以在第三个参数传入greater()得到最小堆。

2.push_heap(_First, _Last):

新添加一个元素在末尾,然后重新调整堆序。也就是把元素添加在底层vector的end()处。

该算法必须是在一个已经满足堆序的条件下,添加元素。该函数接受两个随机迭代器,分别表示first,end,区间范围。

关键是我们执行一个siftup()函数,上溯函数来重新调整堆序。具体的函数机理很简单,可以参考我的编程珠玑里面堆的实现的文章。

3.pop_heap(_First, _Last):

这个算法跟push_heap类似,参数一样。不同的是我们把堆顶元素取出来,放到了数组或者是vector的末尾,用原来末尾元素去替代,然后end迭代器减1,执行siftdown()下溯函数来重新调整堆序。

注意算法执行完毕后,最大的元素并没有被取走,而是放于底层容器的末尾。如果要取走,则可以使用底部容器(vector)提供的pop_back()函数。

4.sort_heap(_First, _Last):

既然每次pop_heap可以获得堆中最大的元素,那么我们持续对整个heap做pop_heap操作,每次将操作的范围向前缩减一个元素。当整个程序执行完毕后,我们得到一个非降的序列。注意这个排序执行的前提是,在一个堆上执行。

下面是这几个函数操作vector中元素的例子。

#include
#include
#include

using namespace std;

int main()
{
    int a[] = {15, 1, 12, 30, 20};
    vector ivec(a, a+5);
    for(vector::iterator iter=ivec.begin();iter!=ivec.end();++iter)
        cout<<*iter<<" ";
    cout<::iterator iter=ivec.begin();iter!=ivec.end();++iter)
        cout<<*iter<<" ";
    cout<::iterator iter=ivec.begin();iter!=ivec.end();++iter)
        cout<<*iter<<" ";
    cout<::iterator iter=ivec.begin();iter!=ivec.end();++iter)
        cout<<*iter<<" ";
    cout<::iterator iter=ivec.begin();iter!=ivec.end();++iter)
        cout<<*iter<<" ";
    cout<

二、priority queue

优先队列(priority_queue)首先是一个queue,那就是必须在末端推入,必须在顶端取出元素。除此之外别无其他存取元素的途径。内部元素按优先级高低排序,优先级高的在前。缺省情况下,priority_heap利用一个max-heap完成,后者是一个以vector表现的完全二叉树。我们说优先队列不是一个STL容器,它以底部容器而实现,修改了接口,形成另一种性质,这样的东西称之为适配器(adapter)。详情参见:http://www.cplusplus.com/reference/stl/priority_queue/


主要函数包括:
bool empty() const 
size_type size() const
const_reference top() const 
        返回顶端元素。不取走。
void push(const value_type& x)
         内部调用push_back(x)和push_heap()
void pop() 
         内部调用pop_heap()和pop_back();

在优先队列中,优先级高的元素先出队列。
标准库默认使用元素类型的<操作符来确定它们之间的优先级关系。
优先队列的第一种用法,也是最常用的用法:

priority_queue qi;

通过<操作符可知在整数中元素大的优先级高。

第二种方法:
在示例1中,如果我们要把元素从小到大输出怎么办呢?
这时我们可以传入一个比较函数,使用functional.h函数对象作为比较函数。

priority_queue, greater >qi2;

其中
第二个参数为容器类型。
第三个参数为比较函数。

第三种方法:
自对自定义的数据类型自定义优先级,

1.通过自定义operator<操作符来比较元素中的优先级。

struct Node
{
       int key;
       int value;
       bool operator <(const Node & a,const Node & b)
       {
              return a.key       }
       bool operator > (const Node & a,const Node & b)
       {
               return a.key>b.key;
       }
};

在该结构中,value为值,priority为优先级,使用示例:

priority_queue,less > pqLess;

priority_queue,greater > pqGreater;


2.自定义一个比较“类”,重载括号,operator (),这这种方式可以由less“继承”。

struct Node
{
        int key;
        int value;
};
struct cmpLess
{
         bool operator ()(const Node & a,const Node & b)
         {
              return a.key         }
};
struct cmpGreater
{
          bool operator ()(const Node & a,const Node & b)
          {
               return a.key>b.key;
          }
};

使用示例:

priority_queue,cmpLess> pqLess;

priority_queue,cmpGreater> pqGreater;


一个简单的例子如下:

#include
#include
#include
#include

using namespace std;


int main()
{

    const int len = 5;
    int i;
    int a[len] = {3,5,9,6,2};
    priority_queue qi;
    for(i = 0; i < len; i++)
        qi.push(a[i]);
    for(i = 0; i < len; i++)
    {
        cout<,less > pq2;
    //priority_queue,cmpLess> pq2;
    vector b(len);
    b[0].key = 6; b[0].value = 1;
    b[1].key = 9; b[1].value = 5;
    b[2].key = 2; b[2].value = 3;
    b[3].key = 8; b[3].value = 2;
    b[4].key = 1; b[4].value = 4;

    for(i = 0; i < len; i++)
        pq2.push(b[i]);
    cout<<"优先级"<<'\t'<<"值"<



参考:http://blog.csdn.net/morewindows/article/details/6967409

            http://www.cppblog.com/shyli/archive/2007/04/06/21366.html


你可能感兴趣的:(C++编程)