【STL】:priority_queue介绍和模拟实现

朋友们、伙计们,我们又见面了,本期来给大家解读一下有关priority_queue的使用,如果看完之后对你有一定的启发,那么请留下你的三连,祝大家心想事成!

C 语 言 专 栏:C语言:从入门到精通

数据结构专栏:数据结构

个  人  主  页 :stackY、

C + + 专 栏   :C++

Linux 专 栏  :Linux

【STL】:priority_queue介绍和模拟实现_第1张图片

目录

1. priority_queue的介绍

2. priority_queue的使用

3. priority_queue的模拟实现


1. priority_queue的介绍

priority_queue详细文档介绍

1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。


2. 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)。


3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。


4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:

  • empty():检测容器是否为空
  • size():返回容器中有效元素个数
  • front():返回容器中第一个元素的引用
  • push_back():在容器尾部插入元素
  • pop_back():删除容器尾部元素

5. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。


6. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。

2. priority_queue的使用

优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。注意:默认情况下priority_queue是大堆。

函数声明 接口说明
priority_queue()/priority_queue(first,last) 构造一个空的优先级队列
empty( ) 检测优先级队列是否为空,是返回true,否则返回
false
top( ) 返回优先级队列中最大(最小元素),即堆顶元素
push(x) 在优先级队列中插入元素x
pop() 删除优先级队列中最大(最小)元素,即堆顶元素

【STL】:priority_queue介绍和模拟实现_第2张图片

1.  默认情况下,priority_queue是大堆

#include 
#include 
#include 
using namespace std;

void Test_priority_queue()
{
	vector v = { 2,5,8,7,4,3,6,9,1,0 };

	//默认情况建的是大堆
	priority_queue heap1;

	for (auto e : v)
	{
		heap1.push(e);
	}
	cout << heap1.top() << endl;
	cout << endl;

	//如果要建小堆可以指定传参
	//容器适配器
	priority_queue, greater> heap2;
	for (auto e : v)
	{
		heap2.push(e);
	}
	cout << heap2.top() << endl;
	cout << endl;
}

2. 如果在priority_queue中放自定义类型的数据,用户需要在自定义类型中提供> 或者< 的重载。

class Date
{
public:
	Date(int year = 2024, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	bool operator<(const Date& d)const
	{
		return (_year < d._year) ||
			(_year == d._year && _month < d._month) ||
			(_year == d._year && _month == d._month && _day < d._day);
	}
	bool operator>(const Date& d)const
	{
		return (_year > d._year) ||
			(_year == d._year && _month > d._month) ||
			(_year == d._year && _month == d._month && _day > d._day);
	}
	friend ostream& operator<<(ostream& _cout, const Date& d)
	{
		_cout << d._year << "-" << d._month << "-" << d._day;
		return _cout;
	}
private:
	int _year;
	int _month;
	int _day;
};

void Test_priority_queue2()
{
	// 大堆,需要用户在自定义类型中提供<的重载
	priority_queue q1;
	q1.push(Date(2024, 1, 1));
	q1.push(Date(2023, 10, 28));
	q1.push(Date(1949, 10, 1));
	cout << q1.top() << endl;

	// 如果要创建小堆,需要用户提供>的重载
	priority_queue, greater> q2;
	q2.push(Date(2024, 1, 1));
	q2.push(Date(2023, 10, 28));
	q2.push(Date(1949, 10, 1));
	cout << q2.top() << endl;
}

3. priority_queue的模拟实现

priority_quque也是通过容器适配器来完成的,只不过又多了一个控制大小堆的仿函数

#pragma once
#include 

namespace ywh
{
	template
	class Less
	{
	public:
		bool operator()(const T& x, const T& y)
		{
			return x < y;
		}
	};

	template, class Compare = Less>
	class priority_queue
	{
	public:
		priority_queue()
		{}
		
		void push(const T& x)
		{
			_con.push_back(x);
			adjust_up(_con.size() - 1);
		}

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}
		const T& top()
		{
			return _con[0];
		}

		bool empty()
		{
			return _con.empty();
		}

		size_t size()
		{
			return _con.size();
		}
	private:
		//向下调整
		void adjust_down(int parent)
		{
			Compare com;
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				//if (child + 1 < _con.size() 
				//	//&& _con[child] < _con[child + 1])
				if (child + 1 < _con.size()
					&& com(_con[child], _con[child + 1]))
				{
					++child;
				}

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

		//向上调整
		void adjust_up(int child)
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (com(_con[parent], _con[child]))
					//if (_con[parent] < _con[child])
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}
	private:
		Container _con;
	};
	
}

朋友们、伙计们,美好的时光总是短暂的,我们本期的的分享就到此结束,欲知后事如何,请听下回分解~,最后看完别忘了留下你们弥足珍贵的三连喔,感谢大家的支持!    

你可能感兴趣的:(C++,c++,开发语言,priority_queue)