STL之priority_queue(优先队列)

优先队列

优先队列(priority_queue)是一种用来维护由一组元素构成的集合S的数据结构。优先队列有两种形式,最大优先队列和最小优先队列。最大优先队列的应用:共享计算机系统的作业调度。最小优先队列的应用:基于事件驱动的模拟器。

C++中的priority_queue

在C++中,包含了模板,其实便是一个最大优先队列,下面演示其用法。

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstdlib>
using namespace std;
int main()
{
	priority_queue<int>q;   //定义一个优先队列
	for(int i=1;i<=10;i++)
	 q.push(i);             //元素如队
	cout<<q.top()<<endl;    //返回优先级最高的元素
	q.pop();                //删除优先级最高的元素
	q.empty();              //判断队列是否为空,若为空返回True
	q.size();              //返回元素的数量
	return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_43400575/article/details/84668024