[C++] STL 优先队列 priority_queue

20121117

Copy From:  http://www.cppblog.com/CodeStream/archive/2011/03/25/142700.html

 

STL 中优先队列的使用方法(priority_queue)

基本操作:

       empty() 如果队列为空返回真

pop() 删除对顶元素

push() 加入一个元素

size() 返回优先队列中拥有的元素个数

top() 返回优先队列对顶元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。


使用方法:

头文件

#include <queue>

声明方式

1、普通方法:
     priority_queue<int>q;
    //通过操作,按照元素从大到小的顺序出队

2、自定义优先级:


struct cmp
{
        operator bool ()(int x, int y)     //NOTE 2012-11-18: 这里函数定义有问题
        {
                 return x > y; // x小的优先级高
                 //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高
        }
};
priority_queue<int, vector<int>, cmp>q;//定义方法
//其中,第二个参数为容器类型。第三个参数为比较函数。

 

3、结构体声明方式:
struct node
{
     int x, y;
     friend bool operator < (node a, node b)
     {
            return a.x > b.x; //结构体中,x小的优先级高   
      }
};
priority_queue<node>q;//定义方法

//在该结构中,y为值, x为优先级。
//通过自定义operator<操作符来比较元素中的优先级。
//在重载”<”时,最好不要重载”>”,可能会发生编译错误

posted on 2012-11-17 00:39  applesun0757  阅读(130)  评论(0)    收藏  举报