STL中的容器适配器priority_queue的用法详解及模拟实现

@TOC

1.priority_queue的介绍和使用

stl源码中,包含三个模板参数:类型、容器适配器、比较规则。

template <class T, class Container = vector<T>, class Compare = less<typename Container::value_type> > 
class 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来自动完成此操作

模拟实现代码展示

priority_queue.h
#include<iostream>
#include<vector>
using namespace std;

template<class T>
struct Less
{
	bool operator()(const T&left, const T&right)
	{
		return left < right;   // 大堆;
	}
};
template<class T>
struct Greater
{
	bool operator()(const T&left, const T&right)
	{
		return left > right;
	}
};
template<class T, class Container = vector<T>, class Compare = Less<T>>
class priority_queue 
{
public:
	priority_queue()   // 普通构造
		:_c() // 无参构造
	{}
	template<class Iterator>
	priority_queue(Iterator front, Iterator back)
		:_c(front,back)
	{
		int n = _c.size();
		for (int i = (n - 1 - 1) / 2; i >= 0; i--)
		{
			AdjustDown(i);
		}
	}

	void push(const T&data)
	{
		_c.push_back(data);  // 这个push_back就是vect
		AdjustUp(_c.size() - 1);
	}
	
	void AdjustUp(int child)    // 这个是在push的时候用
	{
		int parent = (child - 1) >> 1;   //
		while (parent>=0)             //  while (child>0)  
		{
			  //if ( _com.operator()(_c[parent], _c[child])) 
			if ( _com(_c[parent], _c[child])) // 这是上面的简写;
			{
				swap(_c[parent], _c[child]);
				child = parent;
				parent = (child - 1) >> 1;
			}
			else
			{
				break;
			}
		}
	}
	void AdjustDown(int parent)
	{
		int child = (parent << 1) + 1;
		while (child < (int)_c.size())
		{
			if (child+1<size() && _com(_c[child], _c[child + 1]))
			{
				child++;
			}
			if (_com(_c[parent], _c[child]))
			{
				swap(_c[parent], _c[child]);
				parent = child;
				child = parent * 2 + 1;
			}
			else
			{
				break;
			}
		}
	}
	bool empty() const
	{
		return _c.empty();
	}
	const T& top()const
	{
		return _c.front(); // front是vector的方法;
	}
	void pop()
	{
		if (empty())
		{
			return;
		}
		swap(_c.front(), _c.back());
		_c.pop_back();
		AdjustDown(0);
	}
	int size() const
	{
		return _c.size();
	}
private:
	Container _c;
	Compare _com;

};

这里实现了自己写的Less 和Greater类;

less 建大堆,重载()运算符,需要实现小于的比较
greater 建小堆,重载 ()运算符,需要实现大于的比较

称其为仿函数类,原因:使用方式类似于函数调用

  • 1.完整形式:_com.operator()(参数列表) ;这里的_com是我们自己创建的类对象。

  • 2.简写形式:_com(参数列表)

注意:对于这里的大于小于,只能比较内置类型,而对于自定义类型,比如上面的date日期类,就需要在date类的内部重载大于小于。

测试.cc
#include"priority_queue.h"

class Date
{
public:
	Date(int year = 1900, 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);
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& cout, const Date& d) // 不能写在类内,因为写在类内
//第一个参数位置会被this占据;
{
	cout << d._year << "-" << d._month << "-" << d._day;
	return cout;
}
void test1()
{
	//priority_queue<int>pq;             // 默认是大堆(Less)
	priority_queue<int,vector<int>,Greater<int>>pq; // 小堆,<int, ,Greater<int>>pq; 这么写不对只能把中间的也加上;
	pq.push(10);
	pq.push(3);
	pq.push(9);
	pq.push(2);
	pq.push(1);
	pq.push(8);
	pq.push(7);
	pq.push(4);
	while (!pq.empty())
	{
		cout << pq.top() << endl;
		pq.pop();
	}
	cout << endl;
}
void test2()
{ 
	//int arr[] = { 8, 7, 6, 5, 4, 3, 2, 9, 1 };
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	priority_queue<int> pq(arr, arr + 9);
	while (!pq.empty())
	{
		cout << pq.top() << endl;
		pq.pop();
	}
	cout << endl;
}

void test3()   // 测试自定义类型 Date
{
	priority_queue<Date, vector<Date>, Greater<Date>>pq2;  // 从小到达  小堆;
	pq2.push(Date(2018, 2,  9));
	pq2.push(Date(2028, 7,  18));
	pq2.push(Date(2024, 2,  3));
	pq2.push(Date(2021, 9,  13));
	pq2.push(Date(2026, 6,  23));
	while (!pq2.empty())
	{
		cout << pq2.top() << endl;
		pq2.pop();
	}
	cout << endl;
}
int main() 
{
	//test1();
	//test2();
	test3();
	system("pause");
	return 0;
}

在这里插入图片描述
参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值