优先队列

优先队列——priority_queue

介绍

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

使用

首先要包含头文件#include <queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队。

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

基本操作

和队列基本操作相同:

  • top访问队头元素

  • empty队列是否为空

  • size返回队列内元素个数

  • push插入元素到队尾 (并排序)

  • emplace原地构造一个元素并插入队列

  • pop弹出队头元素

  • swap交换内容

定义(初始化)

priority_queue<Type, Container, Functional>

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

当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆。

一般情况如下:

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

例子🌰

默认情况

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include <queue>

using namespace std;

int main() {
    //对于基础类型 默认是大顶堆
    priority_queue<int> a;  //等同于 priority_queue<int, vector<int>, less<int> > a;
    priority_queue<int, vector<int>, greater<int>> c;  //这样就是小顶堆
    priority_queue<string> b;

    for (int i = 0; i < 5; i++) {
        a.push(i);
        c.push(i);
    }
    while (!a.empty()) {
        cout << a.top() << ' ';
        a.pop();
    }
    cout << endl;

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

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

运行结果:

1
2
3
4 3 2 1 0
0 1 2 3 4
cbd abcd abc

用pair做优先队列的元素

使用pair默认比较规则

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <queue>
#include <vector>

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();
    }
}

运行结果:

1
2
3
2 5
1 3
1 2

自定义pair比较规则

可参考博客

  1. 定义比较类,重构operator()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    // 优先队列中使用 pair, 自定义排序
    #define pii pair<int,int>
    
    struct cmp {
        bool operator()(const pii p1, const pii p2) {
            // 大于号是小顶堆,小于号是大顶堆
            return p1.first > p2.first;// first 小的先弹出
        }
    };
    
  2. 定义比较函数:

    1
    2
    3
    4
    5
    6
    
    // 编写比较规则cmp,要求x.first/x.second < y.first/y.second
    // ⚠️:这里比较规则与实际编写的是反向的
    auto cmp = [&](const pair<int, int>& x, const pair<int, int>& y) {
            return arr[x.first] * arr[y.second] > arr[x.second] * arr[y.first];
    };
    priority_queue<pair<int, int>, vector<pair<int, int>>, decltype(cmp)> q(cmp);
    

用自定义类型做优先队列元素的例子

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <queue>

using namespace std;

//方法1-运算符重载<
struct tmp1 {
    int x;

    tmp1(int a) { x = a; }

    bool operator<(const tmp1 &a) const {
        return x < a.x; //大顶堆
    }
};

//方法2-重写仿函数
struct tmp2 {
    bool operator()(tmp1 a, tmp1 b) {
        return a.x < b.x; //大顶堆
    }
};

int main() {
    tmp1 a(1);
    tmp1 b(2);
    tmp1 c(3);
    priority_queue<tmp1> d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) {
        cout << d.top().x << '\n';
        d.pop();
    }
    cout << endl;

    priority_queue<tmp1, vector<tmp1>, tmp2> f;
    f.push(b);
    f.push(c);
    f.push(a);
    while (!f.empty()) {
        cout << f.top().x << '\n';
        f.pop();
    }
}

运行结果:

1
2
3
4
5
6
7
3
2
1
 
3
2
1
0%