C++中priority_queue的实现

代码

#pragma once
#include <vector>
#include <iostream>

using namespace std;

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

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


	template <class T,class Container = vector<T>,class Compare = less<T>>
	class priority_queue {
	public:
		void adjust_up(int child) {
			Compare com;
			int parent = (child - 1) / 2;
			while (child>0) {
				if (com(_con[child] , _con[parent])) {
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else {
					break;
				}
			}
		}

		void adjust_down(size_t parent) {
			Compare com;
			size_t child = parent * 2 + 1 ;
			while (child<_con.size())
			{
				if (child + 1 < _con.size() && com(_con[child] , _con[child+1]))
					++child;
				if (com(_con[parent] , _con[child])) {
					swap(_con[child], _con[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else {
					break;
				}
			}
		}

		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];
		}

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

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

	private:
		Container _con;
	};

	void test_priority_queue()
	{
		//priority_queue<int, vector<int>, greater<int>> pq;
		priority_queue<int> pq;
		//priority_queue<int, deque<int>> pq;
		pq.push(1);
		pq.push(0);
		pq.push(5);
		pq.push(2);
		pq.push(1);
		pq.push(7);

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

	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)
		{
			_cout << d._year << "-" << d._month << "-" << d._day;
			return _cout;
		}

	private:
		int _year;
		int _month;
		int _day;
	};

	class PDateLess
	{
	public:
		bool operator()(const Date* p1, const Date* p2)
		{
			return *p1 < *p2;
		}
	};

	class PDateGreater
	{
	public:
		bool operator()(const Date* p1, const Date* p2)
		{
			return *p1 > *p2;
		}
	};

	void test_priority_queue2()
	{
		// 大堆,需要用户在自定义类型中提供<的重载
		//priority_queue<Date> q1;
		priority_queue<Date, vector<Date>, greater<Date>> q1;
		q1.push(Date(2018, 10, 29));
		q1.push(Date(2018, 10, 30));
		q1.push(Date(2018, 10, 28));
		cout << q1.top() << endl;

		//priority_queue<Date*, vector<Date*>, PDateLess> q2;
		priority_queue<Date*, vector<Date*>, PDateGreater> q2;
		q2.push(new Date(2018, 10, 29));
		q2.push(new Date(2018, 10, 30));
		q2.push(new Date(2018, 10, 28));
		cout << *(q2.top()) << endl;
	}
}

struct和class的区别,什么时候用哪一个?

在定义类的时候 用class和struct都是可以的 这两个就是默认的访问权限不一样 class的默认访问权限是private struct的默认访问权限是public
不过定义类在C++中 一般优先使用class 在定义类似于二叉树节点这种结构的时候 用struct居多
另外在模板参数列表中 template 中只能用class 或者 typename 不能用struct

class Compare的作用

有了这个参数以后,可以自己设计Compare的比较方式

对priority_queue的理解

priority_queue是C++中内置建堆的方法,它继承了堆的特性和优点,能够在大量数据中快速找出最大或者最小的k个数。在template的三个参数中

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值