zoukankan      html  css  js  c++  java
  • STL之优先队列 priority_queue

    priority_queue的用法

    https://blog.csdn.net/xiaoquantouer/article/details/52015928


    https://blog.csdn.net/weixin_36888577/article/details/79937886

    #include <queue>
    与queue的区别在于,我们可以自定义其中数据的优先级

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

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

    定义:
    priority_queue<int> a; //对于基础类型 默认是大顶堆,等同于 priority_queue<int, vector<int>, less<int> > a;
    #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.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty())
    {
    cout << b.top() << ' ';
    b.pop();
    }
    cout << endl;
    return 0;
    }

    输出:
    4 3 2 1 0
    0 1 2 3 4
    cbd abcd abc

  • 相关阅读:
    ASP.NET MVC 动态加载图像
    ASP.NET:以域用户身份访问网络资源
    ASP.NET MVC 动态加载 *.ascx
    4月
    3月
    2月
    每天充点小能量
    每天进步一点点
    FreeMarker标签与使用
    eclipse启动tomcat, http://localhost:8080无法访问
  • 原文地址:https://www.cnblogs.com/sunshine1218/p/14641594.html
Copyright © 2011-2022 走看看