STL priority_queue 的基本用法

priority_queue 基本介绍

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

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

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

头文件

和 queue 一样,需要包含 #include

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

定义方法

priority_queue 变量名;

1、Type 就是数据类型。

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

3、Functional 就是比较的方式。

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

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

基本操作

priority_queue 的基本操作类似 STL stack。

加入数据

使用 push() 函数。

#include        // std::cout
#include           // std::priority_queue
int main () {
    std::priority_queue 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        // std::cout
#include           // std::priority_queue
int main () {
    std::priority_queue 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        // std::cout
#include           // std::priority_queue
int main () {
    std::priority_queue 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        // std::cout
#include           // std::priority_queue
int main () {
    std::priority_queue 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        // std::cout
#include           // std::priority_queue
int main () {
    std::priority_queue 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 
#include 
#include 
using namespace std;
int main() {
    //对于基础类型 默认是大顶堆
    priority_queue big;
    //等同于 priority_queue, less > big;
    //这里一定要有空格,不然成了右移运算符↓↓
    priority_queue, greater > small;  //这样就是小顶堆
    priority_queue 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;
}

运行结果如下图。

STL priority_queue 的基本用法_第1张图片

pair 做为数据

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

#include 
#include 
#include 
using namespace std;
int main() {
    priority_queue > a;
    pair b(1, 2);
    pair c(1, 3);
    pair 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 
#include 
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 d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) {
        cout << d.top().x << endl;
        d.pop();
    }
    cout << endl;

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

运行结果如下图所示。

你可能感兴趣的:(STL,#,priority_queue,priority_queue,使用方法)