C++ 优先级队列priority_queue

1、常用接口

底层使用的是堆实现,默认为大堆

2、理解:

优先级队列就是即使插入的是一个乱序的,但是依旧会按照有序的顺序出队列

优先级队列就是会根据大小来出队列,而不是谁在队头就出谁。
优先级队列底层是使用堆实现,,说白了就是堆
所以首先要建堆
向上调整法

关于什么是堆:堆详解以及简单的堆排序(源代码)-CSDN博客

3、模拟实现

实现文件

#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

template<class T>
class Less {

public:
	bool operator()(const T& x, const T& y)
	{
		return x < y;
	}

};

template<class T>
class Greater {

public:
	bool operator()(const T& x, const T& y)
	{
		return  x > y;
	}

};


namespace myspace
{
	template< class T, class Container = vector<T>, class compare = Greater<T> >
	class priority_queue
	{
		public:
		size_t size()
		{
			return _con.size();
		}
		
		bool empty()
		{
			return _con.empty();
		}

		//大堆->升序
		void adjust_up(size_t child)
		{
			compare com;

			//从最后一个开始,往前调整
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//if (_con[parent] < _con[child]  )
				if(com( _con[parent],_con[child]) )
				{
					swap(_con[parent],_con[child]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
			
		}

		void adjust_down(size_t parent)
		{
			compare com;

			//从顶部位置开始向下
			int child = parent * 2 + 1;//左孩子
			while (child < size())
			{

				//if (child + 1 < size()&& _con[child + 1] > _con[child])//如果右孩子存在并且更大
				if (child + 1 < size() && com(_con[child], _con[child + 1]))
				{
					++child;
				}

				// 交换
				if (com(_con[parent],_con[child] ))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
				
			}
			
		}

		void push(const T& val)
		{
			_con.push_back(val);
			//从最后位置开始向上调整
			adjust_up(size() - 1);
		}

		void pop()
		{
			//首尾交换
			swap(_con[0],_con[size() - 1]);
			_con.pop_back();
			//向下调整
			adjust_down(0);
		}

		T& top()
		{
			return _con[0];
		}

	private:
		Container _con;
	};

}

测试文件

#include<queue>
#include"priority_queue.h"

void test_peiority_queue()
{
	std::priority_queue<int,vector<int>,greater<int>> pq;//第三个参数是一个访函数,传递的是一个类型
	pq.push(1);
	pq.push(2);
	pq.push(3);
	pq.push(4);

	int n = pq.size();
	while (n--)
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;

	vector<int> v{9,4,2,4,6,2,4,8,1};
	sort(v.begin(),v.end(),greater<int>());//第三个参数是传的对象,因为是函数模板
	for (auto e : v)
	{
		cout << e << " ";
	}
	cout << endl;
}

void test_priority_queue2()
{
	myspace::priority_queue<int,vector<int> > pq;
	pq.push(1);
	pq.push(2);
	pq.push(3);
	pq.push(4);

	int  n = pq.size();
	while (n--)
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;

}


int main()
{

	//test_peiority_queue();
	test_priority_queue2();
	
	return 0;
}



 

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二十5画生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值