优先级队列priority_queue基本操作

传智扫地僧课程学习笔记。


直接看老师示例代码就行,

#include 
using namespace std;
#include "queue"

void main81()
{
	priority_queue p1; //默认是 最大值优先级队列 
	//priority_queue, less > p1; //相当于这样写
	priority_queue, greater> p2; //最小值优先级队列

	p1.push(33);
	p1.push(11);
	p1.push(55);
	p1.push(22);
	cout <<"队列大小" << p1.size() << endl;
	cout <<"队头" << p1.top() << endl;

	while (p1.size() > 0)
	{
		cout << p1.top() << " ";
		p1.pop();
	}
	cout << endl;

	cout << "测试 最小值优先级队列" << endl;

	p2.push(33);
	p2.push(11);
	p2.push(55);
	p2.push(22);
	
	while (p2.size() > 0)
	{
		cout << p2.top() << " ";
		p2.pop();
	}
}
void main()
{
	main81();
	cout<<"hello..."<



#include 
using namespace std;
#include "queue"

void main81()
{
	priority_queue p1 ; //默认情况下 是 最大值优先级队列 
	priority_queue, less> p2; //提前定义好的预定义函数  谓词
	priority_queue, greater >  p3; //最小值优先级队列

	p1.push(33);
	p1.push(11);
	p1.push(55);
	p1.push(22);

	cout << "队头元素:" << p1.top() < 0 )
	{
		cout << p1.top() << " ";
		p1.pop();
	}

	cout << "测试 最小值优先级队列 " << endl;

	p3.push(33);
	p3.push(11);
	p3.push(55);
	p3.push(22);

	cout << " 最小值优先级队列 队头元素:" << p3.top() < 0 )
	{
		cout << p3.top() << " ";
		p3.pop();
	}

}

void main888()
{
	main81();
	cout<<"hello..."<


你可能感兴趣的:(C++)