13-2.模板复习priority_queue

模板学习

priority_queue,即为优先队列,是一种以数据的优先级对队列数据进行动态排序的一种STL,可以用它来进行堆的操作(其实比起堆来说就是常数大一点,代码短一点而已吧)。

我们主要用其中的以下几种操作(主要是博主太蠢,其他操作基本不怎么用,也不怎么会)

我们首先定义一个优先队列为pq

1.push操作:一般写成pq.push(x);用途是把x元素插入优先队列(队列保证按顺序排好)

2.top操作:一般写作x=pq.top();用途是返回优先队列的队头元素(其实就是堆顶元素)。

3.pop操作:一般写作pq.pop();用途是删除队头元素(堆顶元素),保证优先队列的有序性。

4.size操作:一般写作x=pq.size();用途是返回优先队列优先队列的长度。

好像常用的只有这四个了吧……

来一道模板题,洛谷3378

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
int n,o,x;
priority_queue<int,vector<int>,greater<int> > pq;
int main(){
    scanf("%d",&n);
    int i,j;
    for(i=1;i<=n;++i){
        scanf("%d",&o);
        if(o==1){scanf("%d",&x);pq.push(x);}
        if(o==2){x=pq.top();printf("%d
",x);}
        if(o==3){x=pq.top();pq.pop();}
    }
}
原文地址:https://www.cnblogs.com/lazytear/p/9356892.html