STL priority_queue 的基本用法

priority_queue 基本介绍

priority_queue 是 STL queue 中一部分。普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除。在优先队列中,元素被赋予优先级。当访问元素时,具有最高优先级的元素最先删除。优先队列具有最高级先出 (first in, largest out)的行为特征。

priority_queue 和 queue 不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队。

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

头文件

和 queue 一样,需要包含 #include <queue>。

同时,priority_queue 也是属于标准命名空间,即 using namespace std。

定义方法

priority_queue<Type, Container, Functional> 变量名;

1、Type 就是数据类型。

2、Container 就是容器类型(Container 必须是用数组实现的容器,比如 vector,deque 等等,但不能用 list。STL里面默认用的是vector)。

3、Functional 就是比较的方式。

//升序队列,小顶堆
priority_queue <int, vector<int>, greater<int> > q;
//降序队列,大顶堆
priority_queue <int, vector<int>, less<int> > q;

//greater和less是std实现的两个仿函数(就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了)

基本操作

priority_queue 的基本操作类似 STL stack。

加入数据

使用 push() 函数。

#include <iostream>       // std::cout
#include <queue>          // std::priority_queue
int main () {
    std::priority_queue<int> mypq;

    mypq.push(30);          //队列数据: 30
    mypq.push(100);         //队列数据: 100 30
    mypq.push(25);          //队列数据: 100 30 25
    mypq.push(40);          //队列数据: 100 40 30 25

    return 0;
}

获取顶部数据

使用 top() 函数。

#include <iostream>       // std::cout
#include <queue>          // std::priority_queue
int main () {
    std::priority_queue<int> mypq;

    mypq.push(30);                          //队列数据: 30
    std::cout << mypq.top() << std::endl;   //30
    mypq.push(100);                         //队列数据: 100 30
    std::cout << mypq.top() << std::endl;   //100
    mypq.push(25);                          //队列数据: 100 30 25
    std::cout << mypq.top() << std::endl;   //100
    mypq.push(40);                          //队列数据: 100 40 30 25
    std::cout << mypq.top() << std::endl;   //100

    return 0;
}

删除数据

使用 pop() 函数,只能删除顶部元素。

#include <iostream>       // std::cout
#include <queue>          // std::priority_queue
int main () {
    std::priority_queue<int> mypq;

    mypq.push(30);                          //队列数据: 30
    std::cout << mypq.top() << std::endl;   //30
    mypq.push(100);                         //队列数据: 100 30
    std::cout << mypq.top() << std::endl;   //100
    mypq.pop();                             //队列数据: 30
    mypq.push(25);                          //队列数据: 30 25
    std::cout << mypq.top() << std::endl;   //30
    mypq.push(40);                          //队列数据: 40 30 25
    std::cout << mypq.top() << std::endl;   //40

    return 0;
}

队列中数据大小

使用 size() 函数。

#include <iostream>       // std::cout
#include <queue>          // std::priority_queue
int main () {
    std::priority_queue<int> mypq;

    mypq.push(30);          //队列数据: 30
    std::cout << mypq.size() << std::endl;   //1
    mypq.push(100);         //队列数据: 100 30
    std::cout << mypq.size() << std::endl;   //2
    mypq.push(25);          //队列数据: 100 30 25
    std::cout << mypq.size() << std::endl;   //3
    mypq.push(40);          //队列数据: 100 40 30 25
    std::cout << mypq.size() << std::endl;   //4

    return 0;
}

队列是否为空

使用 empty() 函数判断。

#include <iostream>       // std::cout
#include <queue>          // std::priority_queue
int main () {
    std::priority_queue<int> mypq;

    mypq.push(30);          //队列数据: 30
    mypq.push(100);         //队列数据: 100 30
    mypq.push(25);          //队列数据: 100 30 25
    mypq.push(40);          //队列数据: 100 40 30 25

    int sum=0;
    while (!mypq.empty()) {
        sum += mypq.top();
        mypq.pop();
    }//sum最终结果为100+40+30+25

    return 0;
}

例子

C++ 内置数据类型

#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main() {
    //对于基础类型 默认是大顶堆
    priority_queue<int> big;
    //等同于 priority_queue<int, vector<int>, less<int> > big;
    //这里一定要有空格,不然成了右移运算符↓↓
    priority_queue<int, vector<int>, greater<int> > small;  //这样就是小顶堆
    priority_queue<string> b;

    for (int i = 0; i < 10; i++) {
        big.push(i);
        small.push(i);
    }

    while (!big.empty()) {
        cout << big.top() << ' ';
        big.pop();
    }
    cout << endl;

    while (!small.empty()) {
        cout << small.top() << ' ';
        small.pop();
    }
    cout << endl;

    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty()) {
        cout << b.top() << ' ';
        b.pop();
    }
    cout << endl;

    return 0;
}

运行结果如下图。

pair 做为数据

比较规则为: 先比较 pair 第一个元素,第一个相等比较第二个。

#include <iostream>
#include <queue>
#include <utility>
using namespace std;
int main() {
    priority_queue<pair<int, int> > a;
    pair<int, int> b(1, 2);
    pair<int, int> c(1, 3);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty()) {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
    return 0;
}

运行结果如下图所示。

自定义数据类型

#include <iostream>
#include <queue>
using namespace std;

//方法1
typedef struct _st1  {
    int x;
    _st1(int a) {x = a;}
    bool operator<(const _st1& a) const {
        //运算符重载<
        return x < a.x; //大顶堆
    }
} ST1;

//方法2
typedef struct _st2 {
    //重写仿函数
    bool operator() (_st1 a, _st1 b) {
        return a.x < b.x; //大顶堆
    }
} ST2;

int main()
{
    ST1 a(6);
    ST1 b(2);
    ST1 c(13);

    priority_queue<ST1> d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) {
        cout << d.top().x << endl;
        d.pop();
    }
    cout << endl;

    priority_queue<ST1, vector<ST1>, ST2> f;
    f.push(b);
    f.push(c);
    f.push(a);
    while (!f.empty()) {
        cout << f.top().x << endl;
        f.pop();
    }
    
    return 0;
}

运行结果如下图所示。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力的老周

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

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

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

打赏作者

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

抵扣说明:

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

余额充值