Stack和Queue(3)

Stack和Queue(3)

priority_queue的模拟实现

priority_queue.h

C++ 复制代码
#include <vector>

namespace soobin
{
	template<class T, class Container = vector<T>>
	class priority_queue
	{
	public:
		//强制生成默认构造
		priority_queue() = default;
		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			:_con(first, last)
		{
			//建堆
			//起始位置是最后一个非叶子节点的位置
			for (int i = (_con.size() - 1 - 1) / 2; i > 0; i--)
			{
				AdjustDown(i);
			}
		}
		void AdjustUp(int child)
		{
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if (_con[child] > _con[parent])
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (parent - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}

		void AdjustDown(int parent)
		{
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				// 假设法,选出左右孩子中小的那个孩子
				if (child + 1 < _con.size() && _con[child + 1] > _con[child])
				{
					++child;
				}

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

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();

			AdjustDown(0);
		}

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

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

		size_t size()
		{
			return _con.size();
		}

	private:
		Container _con;
	};
}

Test.cpp

C++ 复制代码
#include<iostream>
#include<stack>
#include<queue>

using namespace std;

#include "priority_queue.h"

int main()
{
	int a[] = { 1,4,2,5,6,3,2 };
	soobin::priority_queue<int> pq1(a, a + sizeof(a) / sizeof(int));
	//默认是大的优先级高
	soobin::priority_queue<int> pq;
	//小的优先级高的写法
	//priority_queue<int, vector<int>, less<int>> pq;
	pq.push(1);
	pq.push(2);
	pq.push(3);
	pq.push(4);

	while (!pq.empty())
	{
		cout << pq.top() << " ";
		pq.pop();
	}
	cout << endl;
	return 0;
}

仿函数

例子如下:

C++ 复制代码
template <class T>
	struct less 
	{
		bool operator() (const T& x, const T& y) const
		{ 
			return x < y;
		}
	};

	template <class T>
	struct greater
	{
		bool operator() (const T& x, const T& y) const
		{
			return x > y;
		}
	};

是结构体,但是通过operator()进行重载

两个地方的应用:

1.比较大小怕与库里面的函数冲突

拿上面建堆来举例子:

C++ 复制代码
#include <vector>

namespace soobin
{
	template <class T>
	struct less
	{
		bool operator() (const T& x, const T& y) const
		{
			return x < y;
		}
	};

	template <class T>
	struct greater
	{
		bool operator() (const T& x, const T& y) const
		{
			return x > y;
		}
	};
	template<class T, class Container = vector<T>, class Compare = less<T>>
	//template<class T, class Container = vector<T>>
	class priority_queue
	{
	public:
		//强制生成默认构造
		priority_queue() = default;
		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			:_con(first, last)
		{
			//建堆
			//起始位置是最后一个非叶子节点的位置
			for (int i = (_con.size() - 1 - 1) / 2; i > 0; i--)
			{
				AdjustDown(i);
			}
		}
		void AdjustUp(int child)
		{
			Compare com;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//if (_con[child] > _con[parent])
				if(com(_con[child],_con[parent]))
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (parent - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);
			AdjustUp(_con.size() - 1);
		}

		void AdjustDown(int parent)
		{
			Compare com;
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				// 假设法,选出左右孩子中小的那个孩子
				//if (child + 1 < _con.size() && _con[child + 1] > _con[child])
				if (child + 1 < _con.size() && com(_con[child], _con[child + 1]))
				{
					++child;
				}

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

		void pop()
		{
			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();

			AdjustDown(0);
		}

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

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

		size_t size()
		{
			return _con.size();
		}

	private:
		Container _con;
	};
}

2.想要实现比较大小,但是不是我们预期效果的时候

C++ 复制代码
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)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}
C++ 复制代码
int main()
{
	
	soobin::priority_queue<Date*> q1;

	q1.push(new Date{ 2024, 10, 23});
	q1.push(new Date{ 2024, 5, 27 });
	q1.push(new Date{ 2024, 11, 2 });

	while (!q1.empty())
	{
		cout << *q1.top() << " ";
		q1.pop();
	}
	cout << endl;

	return 0;
}

我们在实现上面日期类的时候,如果不去自己设置仿函数,编译器可能会比较指针,而不是里面的内容,需要自己去设置仿函数比较里面内容的大小

相关推荐
CC__xy3 小时前
demo 通讯录 + 城市选择器 (字母索引左右联动 ListItemGroup+AlphabetIndexer)笔记
windows
No0d1es4 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
DjangoJason5 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq
大阳1236 小时前
线程(基本概念和相关命令)
开发语言·数据结构·经验分享·算法·线程·学习经验
weixin_307779137 小时前
VS Code配置MinGW64编译GNU 科学库 (GSL)
开发语言·c++·vscode·算法
学行库小秘7 小时前
ANN神经网络回归预测模型
人工智能·python·深度学习·神经网络·算法·机器学习·回归
没落之殇8 小时前
基于C语言实现的HRV分析方法 —— 与Kubios和MATLAB对比
算法
FPGA8 小时前
探讨4B/5B编码、8B/10B编码区别以及FPGA实现
数据结构
秋难降8 小时前
线段树的深度解析(最长递增子序列类解题步骤)
数据结构·python·算法
楚韵天工8 小时前
基于GIS的无人机模拟飞行控制系统设计与实现
深度学习·算法·深度优先·无人机·广度优先·迭代加深·图搜索算法