优先队列(priority_queue)用法详解

c++优先队列(priority_queue)用法详解_c++ 优先队列_吕白_的博客-CSDN博客

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

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

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

定义:priority_queue<Type, Container, Functional>
Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆,头顶元素最大,由大到小排序

基本类型

#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;
    /*
    输出:4 3 2 1 0
    */
    while (!c.empty())
    {
        cout << c.top() << ' ';
        c.pop();
    }
    cout << endl;
    /*
   输出:0 1 2 3 4
   */
    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty())
    {
        cout << b.top() << ' ';
        b.pop();
    }
    cout << endl;
    /*
    输出:cbd abcd abc
    */
    system("pause");
    return 0;
}

pair比较

#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 = make_pair(1, 2);
    a.push(b);
    a.push(c);
    a.push(pair<int, int> (1, 3));
    a.push(make_pair(2,5));
    while (!a.empty())
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
    /*
    输出:
            2 5
            1 3
            1 2
            1 2
    */
    system("pause");
    return 0;
}

自定义数据类型

#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; //大顶堆
        return x > a.x; //小顶堆
    }
};

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

int main()
{
    tmp1 a(3);
    tmp1 b(1);
    tmp1 c(2);
    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;
    /*
    输出1 
        2 
        3
    */
    system("pause");
    return 0;
}
------------------------------------------------------------------------------------

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <list>
#include <queue>
using namespace std;


struct tmp1 //运算符重载<
{
    int x;
    tmp1(int a) { x = a; }
    //bool operator<(const tmp1& a) const
    //{
    //    //return x < a.x; //大顶堆
    //    return x > a.x; //小顶堆
    //}
};

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

int main()
{
    tmp1 a(3);
    tmp1 b(1);
    tmp1 c(2);
    priority_queue<tmp1, vector<tmp1>, tmp2> f;// 放函数不可以
    f.push(c);
    f.push(b);
    f.push(a);
    while (!f.empty())
    {
        cout << f.top().x << '\n';
        f.pop();
    }
    /*
   输出1
       2
       3
   */
    system("pause");
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值