c++ 优先队列 priority_queue

参考博客:https://www.cnblogs.com/huashanqingzhu/p/11040390.html

#include<iostream>
#include<vector>
#include<string>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include <stack>
#include<queue>
#include <map>

using namespace std;

int main()
{
    // 对于基础类型 默认是大顶堆
    priority_queue<int> default_priority_queue;
    // 等同于 priority_queue<int, vector<int>, less<int> > a;
    // 这里一定要有空格,不然成了右移运算符↓↓
    priority_queue<int, vector<int>, greater<int> > greater_priority_queue;  //这样就是小顶堆

    int a[5] = { 1,3,2,5,4 };
    for (int i = 0; i != 5; i++)
    {
        default_priority_queue.push(a[i]);
        greater_priority_queue.push(a[i]);
    }

    while (default_priority_queue.size())
    {
        cout << default_priority_queue.top() << " ";
        default_priority_queue.pop();
    }
    cout << endl;

    while (greater_priority_queue.size())
    {
        cout << greater_priority_queue.top() << " ";
        greater_priority_queue.pop();
    }
    cout << endl;
    return 0;
}
文章目录