C++中的priority_queue使用方法

priority_queue是C++中queue库中的优先队列,语法如下:

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

优先队列具有队列的所有特性,包括队列的基本操作,只是在这基础上添加了内部的一个排序,它本质是一个堆实现的。

它和队列基本操作相同:

  • top 访问队头元素
  • empty 队列是否为空
  • size 返回队列内元素个数
  • push 插入元素到队尾 (并排序)
  • emplace 原地构造一个元素并插入队列
  • pop 弹出队头元素
  • swap 交换内容

优先队列的使用方法如下:

//默认是大顶堆存储
priority_queue <int> pq;

//升序队列,小顶堆
priority_queue <int, vector<int>, greater<int> > pq;
//降序队列,大顶堆
priority_queue <int, vector<int>, less<int> > pq;
//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

实例如下:

#include <iostream>
#include <queue>

using namespace std;

int main(){
    int arr[10] = {3, 2, 54, 1, 66, 77, 44, 8, 0, 10};
    priority_queue<int> pq;
    for(int i = 0; i < 10; i++) pq.push(arr[i]);
    for(int i = 0; i < 10; i++){
        cout<<pq.top()<<" ";
        pq.pop();
    }
    return 0;
}

运行结果如下:
在这里插入图片描述
可以看到,元素从大到小排列,即默认是大顶堆。如果要从小到大,可以使用:

priority_queue<int, vector<int>, greater<int>> pq;

运行结果如下:
在这里插入图片描述

当然,我们可以自己定义一个比较方法,这个适用于我们自己定义的数据结构中。示例如下:
首先我们定义如下结构体:

struct complex{
    int real;
    int imag;
    complex(int r, int i): real(r), imag(i){}
    bool operator<(complex c) const {
        if(real * real + imag * imag == c.real * c.real + c.imag * c.imag)
            return imag > c.imag;
        else return real * real + imag * imag < c.real * c.real + c.imag * c.imag;
    }
};

即复数,这里面我们重载了<号,要求是首先根据模比较大小,若是模相等,则虚部大的复数要小,然后我们使用默认的优先队列,即大顶堆,代码如下:

#include<iostream>
#include<queue>

using namespace std;

struct complex{
    int real;
    int imag;
    complex(int r, int i): real(r), imag(i){}
    bool operator<(complex c) const {
        if(real * real + imag * imag == c.real * c.real + c.imag * c.imag)
            return imag > c.imag;
        else return real * real + imag * imag < c.real * c.real + c.imag * c.imag;
    }
};

int main(){
    priority_queue<complex> pq;
    int a[5] = {3, 4, 5, 2, 1};
    int b[5] = {4, 3, 7, 2, 5};
    for(int i = 0; i < 5; i++) pq.push(complex(a[i], b[i]));
    for(int i = 0; i < 5; i++){
        complex c = pq.top();
        pq.pop();
        cout<<c.real<<"+"<<c.imag<<"i"<<endl;
    }
    return 0;
}

运行结果如下:
在这里插入图片描述
可以看到,其中的4+3i与3+4i的模是相等的,但是因为3i < 4i,所以4+3i > 3+4i。当然,我们也可以按照升序输出,那么这里就需要重载>号:

#include<iostream>
#include<queue>

using namespace std;

struct complex{
    int real;
    int imag;
    complex(int r, int i): real(r), imag(i){}
    bool operator<(complex c) const {
        if(real * real + imag * imag == c.real * c.real + c.imag * c.imag)
            return imag > c.imag;
        else return real * real + imag * imag < c.real * c.real + c.imag * c.imag;
    }
    bool operator>(complex c) const {
        if(real * real + imag * imag == c.real * c.real + c.imag * c.imag)
            return imag < c.imag;
        else return real * real + imag * imag > c.real * c.real + c.imag * c.imag;
    }
};

int main(){
    priority_queue<complex, vector<complex>, greater<complex>> pq;
    int a[5] = {3, 4, 5, 2, 1};
    int b[5] = {4, 3, 7, 2, 5};
    for(int i = 0; i < 5; i++) pq.push(complex(a[i], b[i]));
    for(int i = 0; i < 5; i++){
        complex c = pq.top();
        pq.pop();
        cout<<c.real<<"+"<<c.imag<<"i"<<endl;
    }
    return 0;
}

运行结果如下:
在这里插入图片描述

  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花无凋零之时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值