优先队列 priority_queue

在<queue>头文件中,还定义了另一个非常有用的模板类priority_queue(优先队列)。优先队
列与队列的差别在于优先队列不是按照入队的顺序出队,而是按照队列中元素的优先权顺序
出队(默认为大者优先,也可以通过指定算子来指定自己的优先顺序)。
priority_queue 模板类有三个模板参数,第一个是元素类型,第二个容器类型,第三个是比
较算子。其中后两个都可以省略,默认容器为vector,默认算子为less,即小的往前排,大
的往后排(出队时序列尾的元素出队)。
定义priority_queue 对象的示例代码如下:
priority_queue<int> q1;
priority_queue< pair<int, int> > q2; // 注意在两个尖括号之间一定要留空格。
priority_queue<int, vector<int>, greater<int> > q3; // 定义小的先出队
priority_queue 的基本操作与queue 相同。


初学者在使用priority_queue 时,最困难的可能就是如何定义比较算子了。
如果是基本数据类型,或已定义了比较运算符的类,可以直接用STL 的less 算子和greater
算子——默认为使用less 算子,即小的往前排,大的先出队。
如果要定义自己的比较算子,方法有多种,这里介绍其中的一种:重载比较运算符。优先队
列试图将两个元素x 和y 代入比较运算符(对less 算子,调用x<y,对greater 算子,调用x>y),
若结果为真,则x 排在y 前面,y 将先于x 出队,反之,则将y 排在x 前面,x 将先出队。

首先要添加头文件:#include <functional>

priority_queue< int,vector<int>,greater<int> > q; // 注意:> > 误写成>> 会报错

这样我们就创建了一个低优先级元素先出对列的优先队列。

修改上面的例子,运行,就会发现,输出的顺序变成了:1 2 3 4 5。

当然,与greater相对的less,如果传入less这个比较函数,那么就是高优先级元素先出队列了。

priority_queue< int,vector<int>,less<int> > q; 

priority_queue<int> q;

以上创建的优先队列是相同的。



看下面这个简单的示例:

#include <iostream>
#include <queue>
using namespace std;
class T
{
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator < (const T &t1, const T &t2)
{
return t1.z < t2.z; // 按照z 的顺序来决定t1 和t2 的顺序
}
main()
{
priority_queue<T> q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top(); q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}

输出结果为(注意是按照z 的顺序从大到小出队的):
3 3 6
2 2 5
1 5 4
4 4 3
再看一个按照z 的顺序从小到大出队的例子:


#include <iostream>
#include <queue>
using namespace std;
class T
{
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator > (const T &t1, const T &t2)
{
return t1.z > t2.z;
}
main()
{
priority_queue<T, vector<T>, greater<T> > q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top(); q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}


输出结果为:
4 4 3
1 5 4
2 2 5
3 3 6
如果我们把第一个例子中的比较运算符重载为:
bool operator < (const T &t1, const T &t2)
{
return t1.z > t2.z; // 按照z 的顺序来决定t1 和t2 的顺序
}

则第一个例子的程序会得到和第二个例子的程序相同的输出结果。


STL 中优先队列的使用详细介绍(priority_queu)

   #include <queue>

基本操作:

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

pop() 删除对列首元素

push() 加入一个元素

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

top() 返回优先队列首元素

在默认的优先队列中,优先级高的先出队。

标准库默认使用元素类型的 < 操作符来确定它们之间的优先级关系。


以下是自己总结的代码

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
using namespace std;
#include <queue>

struct cmp{                     ///   priority_queue<int,vector<int>,cmp >x; 自己定义cmp
int operator () (int a,int b)
{
    return a>b;
}
};


struct Line{
  int x,y,z;
  friend int operator < (Line n1,Line n2)   ///  比较算子 less
  {
      return n1.z < n2.z;
  }

  friend int operator > (Line n1,Line n2)   ///  比较算子 greater
  {
      return n1.z > n2.z;
  }

}w[3];

int main()
{
/// priority_queue<int>a;          ///  这三种一个意思  默认的是less
/// priority_queue<int,vector<int> >a;
    priority_queue<int,vector<int> ,less<int> >a;
    a.push(5);             /// 注意不能写成 less<int>>a; 要有空格
    a.push(4);
    a.push(3);
    a.push(2);
    while(!a.empty()){
        cout<<a.top()<<endl;
        a.pop();
    }
    printf("\n");



/// priority_queue<Line>t;                /// 同上
/// priority_queue<Line,vector<Line> >t;
    priority_queue<Line,vector<Line>, less<Line> >t;
    w[0].x=4;w[0].y=5;w[0].z=9;
    w[1].x=6;w[1].y=1;w[1].z=3;
    w[2].x=4;w[2].y=5;w[2].z=6;
    t.push(w[0]);
    t.push(w[1]);
    t.push(w[2]);
    while(!t.empty()){
        cout<<t.top().x<<"-"<<t.top().y<<"-"<<t.top().z<<endl;
        t.pop();
    }
    printf("\n");



    priority_queue<Line,vector<Line>, greater<Line> >t1;
    w[0].x=4;w[0].y=5;w[0].z=9;
    w[1].x=6;w[1].y=1;w[1].z=3;
    w[2].x=4;w[2].y=5;w[2].z=6;
    t1.push(w[0]);
    t1.push(w[1]);
    t1.push(w[2]);
    while(!t1.empty()){
        cout<<t1.top().x<<"-"<<t1.top().y<<"-"<<t1.top().z<<endl;
        t1.pop();
    }
    printf("\n");



    priority_queue<int,vector<int>,cmp >x;
    x.push(3);
    x.push(9);
    x.push(2);
    x.push(6);
    while(!x.empty())
    {
        cout<<x.top()<<endl;
        x.pop();
    }
    printf("\n");

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值