优先级队列priority_queue基本操作

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


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

#include <iostream>
using namespace std;
#include "queue"

void main81()
{
	priority_queue<int> p1; //默认是 最大值优先级队列 
	//priority_queue<int, vector<int>, less<int> > p1; //相当于这样写
	priority_queue<int, vector<int>, greater<int>> 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..."<<endl;
	system("pause");
	return ;
}



#include <iostream>
using namespace std;
#include "queue"

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

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

	cout << "队头元素:" << p1.top() <<endl;
	cout << "队列的大小:" << p1.size() << endl;

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

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

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

	cout << " 最小值优先级队列 队头元素:" << p3.top() <<endl;
	cout << " 最小值优先级队列 队列的大小:" << p3.size() << endl;

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

}

void main888()
{
	main81();
	cout<<"hello..."<<endl;
	system("pause");
	return ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值