Simple to use C ++ STL priority queue (The priority_queue) of the container

Simple to use C ++ STL priority queue (The priority_queue) of the container

If you need to use local heap, you can use C ++ STL priority_queue realize the more convenient

/*
priority_queue <type, containor, function>
param type	    : 每个元素的数据类型
param containor : 底层用什么结构存储数据,默认是vector
param function 	: 比较大小的函数
return 			: 优先队列对象
*/
priority_queue <int, vector<int>, greater<int> > q;

/*
param x : 向堆中插入元素
return  :  ----
*/
q.push(x);

/*
param  : --
return : 堆顶的元素
*/
q.top();

/*
param  : --
return : --
作用:删除堆顶元素
*/
q.pop();

Examples

#include <iostream>
#include <queue>

using namespace std;

int temp[114514];

int main()
{
	priority_queue <int, vector<int>, greater<int> > q;
	
	int n, x;
	cin>>n;
	
	for(int i=0; i<n; i++)
	{
		cin>>x;
		q.push(x);
	}
	
	while(!q.empty())
	{
		cout<<q.top()<<" ";
		q.pop();
	}
	cout<<endl;
	
	return 0;
}

Here Insert Picture Description

Published 30 original articles · won praise 1 · views 279

Guess you like

Origin blog.csdn.net/weixin_44176696/article/details/104054795