priority_Queue优先级队列-- 容器适配器

在这里插入图片描述

priority_Queue–没有template模板

单纯实现随意形成大堆和小堆

#pragma once
#include <vector>
namespace bit
{
	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;
		}
	};
	// 模板参数 -- 类型
	// 函数参数 -- 对象
	// less -- 大堆
	//template<class T, class Container = vector<T>, class Compare = Less<T>>
	template<class T, class Container = vector<T>, class Compare = less<typename Container::value_type>>//value_type即T,typename后面加的是类型
	class priority_queue
	{
	public:
		加了inputIterator才需要加这个默认构造
		//priority_queue()
		//{}
		//template<class InputIterator>
		//priority_queue(InputIterator first, InputIterator last)
		//{
		//	// 数据插入进去
		//	while (first != last)
		//	{
		//		_con.push_back(*first);
		//		++first;
		//	}
		//	// 建堆
		//	for (int i = (_con.size() - 1 - 1) / 2; i >= 0; --i)
		//	{
		//		AdjustDwon(i);
		//	}
		//}
		void AdjustUp(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 AdjustDwon(size_t 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[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}
		void push(const T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}
		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdjustDwon(0);
		}
		const T& top() const//,出了作用域返回对象还在最好加引用 且返回别名防止修改最好加引用
		{
			return _con[0];
		}
		size_t size()
		{
			return _con.size();
		}
		bool empty()
		{
			return _con.empty();
		}
	private:
		Container _con;
	};
 
	void test_priority_queue()
	{
		// 默认是大堆        -- 大的优先级高
		//priority_queue<int> pq; 
		// 如果想控制成一个小堆 -- 小的优先级高
		priority_queue<int, vector<int>, Greater<int>> pq;
		//list<int> lt = { 3, 4, 5, 6, 2, 10 };
		//priority_queue<int, vector<int>, greater<int>> pq(lt.begin(), lt.end());
		pq.push(1);
		pq.push(10);
		pq.push(11);
		pq.push(3);
		pq.push(5);
		pq.push(8);
		cout << pq.top() << endl;
		while (!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}
}

在这里插入图片描述

priority_Queue有template模板–用List来初始化

#pragma once
#include <vector>
namespace bit
{
	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;
		}
	};
	//template<class T, class Container = vector<T>, class Compare = Less<T>>
	template<class T, class Container = vector<T>, class Compare = less<typename Container::value_type>>//value_type即T,typename后面加的是类型
	class priority_queue
	{
	public:
		加了inputIterator才需要加这个默认构造
		priority_queue()
		{}
		template<class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			// 数据插入进去
			while (first != last)
			{
				_con.push_back(*first);
				++first;
			}
			// 建堆
			for (int i = (_con.size() - 1 - 1) / 2; i >= 0; --i)
			{
				AdjustDwon(i);
			}
		}
		void AdjustUp(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 AdjustDwon(size_t 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[parent], _con[child]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
				{
					break;
				}
			}
		}
		void push(const T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}
		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			AdjustDwon(0);
		}
		const T& top() const//,出了作用域返回对象还在最好加引用 且返回别名防止修改最好加引用
		{
			return _con[0];
		}
		size_t size()
		{
			return _con.size();
		}
		bool empty()
		{
			return _con.empty();
		}
	private:
		Container _con;
	};
	void test_priority_queue()
	{
		//Less less;
		cout << less.operator()(1,2) << endl;
		//cout << less(1, 2) << endl;

		//Less<int> less;//有int的成员变量的时候
		cout << less.operator()(1,2) << endl;
		//cout << less(1, 2) << endl;

		//Greater<int> greater;
		cout << greater.operator()(1,2) << endl;
		//cout << greater(1, 2) << endl;

		// 默认是大堆        -- 大的优先级高
		//priority_queue<int> pq; 
		// 如果想控制成一个小堆 -- 小的优先级高
		//priority_queue<int, vector<int>, Greater<int>> pq;
		//用其他容器的迭代器来初始化
		list<int> lt = { 3, 4, 5, 6, 2, 10 };
		priority_queue<int, vector<int>, greater<int>> pq(lt.begin(), lt.end());
		pq.push(1);
		pq.push(10);
		pq.push(11);
		pq.push(3);
		pq.push(5);
		pq.push(8);
		cout << pq.top() << endl;
		while (!pq.empty())
		{
			cout << pq.top() << " ";
			pq.pop();
		}
		cout << endl;
	}
}

在这里插入图片描述

test.c

#include <iostream>
#include <stack>
#include <queue>
#include <forward_list>
#include <algorithm>
#include <vector>
#include <list>
#include <deque>
#include <functional>
using namespace std;
#include "PriorityQueue.h"
int main()
{
	bit::test_priority_queue();
	return 0;
}

对比两个priority_Queue

在这里插入图片描述

反函数实现的过程

在这里插入图片描述

输出最大日期

T->Date

// 仿函数的变异玩法 -- 我们可以通过仿函数控制比较方式 
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);
	}
	friend ostream& operator<<(ostream& _cout, const Date& d);
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day << endl;
	return _cout;
}
int main()
{
	priority_queue<Date> pq;
	pq.push( Date(2023, 11, 24));
	pq.push(Date(2021, 10, 24));
	pq.push(Date(2021, 12, 24));
	pq.push( Date(2022, 1, 24));
	cout << (pq.top()) << endl;
	pq.pop();
	return 0;
}

在这里插入图片描述

T->Date*

// 仿函数的变异玩法 -- 我们可以通过仿函数控制比较方式 
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);
	}
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend class PDateLess;
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day << endl;
	return _cout;
}
class PDateLess
{
public:
    //有友缘运用才能访问Date类私有成员变量
	/*bool operator()(const Date* p1, const Date* p2)
	{
	if (p1->_year < p2->_year ||
	(p1->_year == p2->_year && p1->_month < p2->_month) ||
	(p1->_year == p2->_year && p2->_month == p2->_month && p1->_day < p2->_day))
	{
	return true;
	}
	else
	{
	return false;
	}
	}*/
	//Date类有比较大小的写友缘运用就不用写屏蔽掉的代码了
	bool operator()(const Date* p1, const Date* p2)
	{
		return *p1 < *p2;//对象比较大小
	}
};
int main()
{
	priority_queue<Date*, vector<Date*>, PDateLess> pq;//需要写成Date*则需要写vector<Date*>,PDateLess为类型
	pq.push(new Date(2023, 11, 24));
	pq.push(new Date(2021, 10, 24));
	pq.push(new Date(2021, 12, 24));
	pq.push(new Date(2022, 1, 24));
	cout << (*pq.top()) << endl;
	pq.pop();
	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值