优先对列(priority_queue)的介绍和使用

本文介绍了C++中的优先队列priority_queue,它是一种容器适配器,基于堆实现,允许快速访问最大元素。文章详细阐述了priority_queue的原理,包括其底层容器的默认选择(vector)及对迭代器的要求。此外,还讨论了如何使用priority_queue,特别是对于自定义类型的元素,需要提供比较操作符。最后提到了priority_queue的模拟实现和模板参数的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. priority_queue的介绍
priority_queue文档介绍
翻译:

  1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
  2. 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)。
  3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。
  4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭代器访问,并支持以下操作:
  • empty():检测容器是否为空
  • size():返回容器中有效元素个数
  • front():返回容器中第一个元素的引用
  • push_back():在容器尾部插入元素
  • pop_back():删除容器尾部元素
  1. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。
  2. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此操作。

2. priority_queue的使用

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

  2. 使用

void test()
{
	//默认大堆实现
	//priority_queue<int> pq;
	//小堆
	priority_queue<int, vector<int>, greater<int>> pq;
	pq.push(100);
	pq.push(1);
	pq.push(125);
	pq.push(10);
	pq.push(0);
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}
  1. 利用模拟仿函数实现大堆小堆
#include<list>
#include<stack>
#include<iostream>
#include<vector>
#include<queue>
#include<deque>
#include<functional>
using namespace std;
//仿函数类:重载“()”运算符
template<class T>
struct Greater
{
	bool operator()(const T& a, const T& b)
	{
		return a > b;
	}
};
//仿函数类
template<class T>
struct Less
{
	bool operator()(const T& a, const T& b)
	{
		return a < b;
	}
};
//添加仿函数类为参数,实现优先级队列的大堆,小堆
//优先级队列默认用vector实现,也支持deque,不支持list
void test()
{
	//大堆实现
	//priority_queue<int, vector<int>,Less<int>> pq;
	//小堆实现
	priority_queue<int, vector<int>, Greater<int>> pq;
	//priority_queue<int, deque<int>> pq;
	//不支持list,list不支持随机访问
	pq.push(100);
	pq.push(1);
	pq.push(125);
	pq.push(10);
	pq.push(0);
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}
  1. 如果在priority_queue中放自定义类型的数据,用户需要在自定义类型中提供> 或者< 的重载。
//自定义类型
class A
{
public:
	A(int a=1)
		:_a(a)
	{}
	//大堆比较
	bool operator<(const A& a) const
	{
		return _a < a._a;
	}
	//小堆比较
	bool operator>(const A& a) const
	{
		return _a > a._a;
	}
public:
	int _a;
};

void test()
{
	//优先级队列存放自定义类型:需要自定义类型支持比较的操作
	//priority_queue<A> pq;  //大堆
	priority_queue<A, vector<A>, greater<A>> pq;  //小堆
	pq.push(A(1));
	pq.push(A(10));
	pq.push(A(15));
	pq.push(A(2));
	pq.push(A(0));
	while (!pq.empty())
	{
		cout << pq.top()._a << " ";
		pq.pop();
	}
	cout << endl;
}
  1. 优先级队列默认用vector实现,也支持deque,不支持list
void test()
{
	//默认实现大堆
	//priority_queue<int, vector<int>> pq;
	priority_queue<int, deque<int>> pq;
	//不支持list,list不支持随机访问
	pq.push(100);
	pq.push(1);
	pq.push(125);
	pq.push(10);
	pq.push(0);
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}

3.优先对列的模拟实现

//优先级对列实现
//实现大堆结构
template <class T>
class PriorityQueue
{
public:
	//堆:向下调整
	void shiftDown()
	{
		int parent = 0;
		int child = 2 * parent + 1;
		while (child < arr.size())
		{
			if (child + 1 < arr.size() && arr[child] < arr[child + 1])
				child++;
			if (arr[parent] < arr[child])
			{
				swap(arr[parent], arr[child]);

				//更新位置
				parent = child;
				child = 2 * parent + 1;
			}
			else
				break;
		}
	}

	//向上调整
	void shiftUp(int child)
	{
		int parent = (child - 1) / 2;
		while (child > 0)
		{
			if (arr[parent] < arr[child])
			{
				swap(arr[parent], arr[child]);

				//更新位置
				child = parent;
				parent = (child - 1) / 2;
			}
			else
				break;
		}
	}

	void push(const T& val)
	{
		//尾插
		arr.push_back(val);
		//向上调整
		shiftUp(arr.size() - 1);
	}

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

	T& top()
	{
		return arr.front();
	}

	size_t size() const
	{
		return arr.size();
	}
	bool empty() const
	{
		return arr.empty();
	}
private:
	vector<T> arr;
};

void test()
{
	//默认大堆实现
	PriorityQueue<int> pq;
	//小堆
	//priority_queue<int, vector<int>, greater<int>> pq;
	pq.push(100);
	pq.push(1);
	pq.push(125);
	pq.push(10);
	pq.push(0);
	for (int i = 0; i < 1000; i++)
	{
		pq.push(i);
	}
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}

4.优先对列的模板实现

#include<list>
#include<stack>
#include<iostream>
#include<vector>
#include<queue>
#include<deque>
#include<functional>
using namespace std;
//仿函数类:重载“()”运算符
template<class T>
struct Greater
{
	bool operator()(const T& a, const T& b)
	{
		return a > b;
	}
}; template<class T>
struct Less
{
	bool operator()(const T& a, const T& b)
	{
		return a < b;
	}
};
//用vector实现优先级队列
//实现大堆结构
template <class T,class Container,class Compare>
class PriorityQueue
{
public:
	//堆:向下调整
	void shiftDown()
	{
		int parent = 0;
		int child = 2 * parent + 1;
		while (child < arr.size())
		{
			if (child + 1 < arr.size() && cmp(arr[child],arr[child + 1]))
			//if (child + 1 < arr.size() && arr[child] < arr[child + 1])
				child++;

			if (cmp(arr[parent],arr[child]))
			//if (arr[parent] < arr[child])
			{
				swap(arr[parent], arr[child]);

				//更新位置
				parent = child;
				child = 2 * parent + 1;
			}
			else
				break;
		}
	}

	//向上调整
	void shiftUp(int child)
	{
		int parent = (child - 1) / 2;
		while (child > 0)
		{
			if (cmp(arr[parent],arr[child]))
			//if (arr[parent] < arr[child])
			{
				swap(arr[parent], arr[child]);

				//更新位置
				child = parent;
				parent = (child - 1) / 2;
			}
			else
				break;
		}
	}

	void push(const T& val)
	{
		//尾插
		arr.push_back(val);
		//向上调整
		shiftUp(arr.size() - 1);
	}

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

	T& top()
	{
		return arr.front();
	}

	size_t size() const
	{
		return arr.size();
	}
	bool empty() const
	{
		return arr.empty();
	}
private:
	//vector<T> arr;
	Container arr;
	Compare cmp;
};

void test()
{
	//PriorityQueue<int, vector<int>,Greater<int>> pq;  
	PriorityQueue<int, vector<int>, Less<int>> pq;    
	pq.push(100);
	pq.push(1);
	pq.push(125);
	pq.push(10);
	pq.push(0);
	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值