学习总结_STL_优先队列

本文详细介绍了C++标准库中优先队列(priority_queue)的基本用法及实现原理,包括如何定义不同类型的数据结构作为队列元素,并通过示例展示了如何进行元素的插入、删除等操作。

头文件:

<queue>

 

声明:

数据大的优先级高:priority_queue<int> que;    //数据类型为int型,que为变量名

数据小的优先级高:priority_queue<int, vector<int>, greater<int> > que;  //数据类型为int型

数据类型为结构体:priority_queue<node> que;

#include <queue>
#include <iostream>
#include <cstdio>

using namespace std;

typedef struct node
{
    int weight;
    int value;
}SS;

bool operator<(const node &a,const node &b)
{
    if(a.weight==b.weight) return a.value>b.value;
    else return a.weight>b.weight;
}

int main()
{
    priority_queue<node> que;
    int n;
    cin>>n;
    SS s[55];
    for(int i=0;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        s[i].weight=x;
        s[i].value=y;
        que.push(s[i]);
    }
    while(!que.empty())
    {
        printf("%d %d\n",que.top().weight,que.top().value);
        que.pop();
    }
    return 0;
}


 函数:

empty()  //判断队列是否为空,为空返回true,不为空返回false

top()      //返回值为队列的队首元素

push()  //void型函数,无返回值,将元素入队,如que.push(1024);将1024入队

pop()   //将队首元素删除

size()  //返回值为队列的元素个数


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值