C++ 栈和队列

文章目录

  • 栈和队列相对于以往容器的不同点
  • 模拟实现
  • deque 双端队列
  • priority_queue 优先级队列
    • ★仿函数
    • 模拟实现优先级队列(以及学习仿函数)

栈和队列相对于以往容器的不同点

  1. 栈和队列不是容器,而是容器适配器,故没有迭代器(如果有的话它们不就不能维持这特殊的结构了吗)
  2. 可以看到,list 第二个模板参数是空间配置器,而 stack 并非如此:
    C++ 栈和队列_第1张图片

模拟实现

前言:栈和队列被称为 “适配器”,类比生活中的电源适配器,它不产生电(不自己存储数据),而是转换电为特定的大小(以特定方式管理数据)以便使用
而这种 “将一个类的接口转换成客户希望的另外一个接口” 的方式,就叫做 适配器模式

namespace hazb {
	//栈借助其他容器存储数据
	template<class T, class Container = vector<T>>
	//使用模板,使之可以使用不同的容器以实现不同功能,默认使用vector容器
	class stack {
	public:
		void push(const T &x) {
			_con.push_back(x);
		}
		void pop() {
			_con.pop_back();
		}
		T& top() {
			return _con.back();
		}
		size_t size()
		{
			return _con.size();
		}

		bool empty()
		{
			return _con.empty();
		}
	private:
		Container _con;
	};
}

queue 同理

deque 双端队列

在上面的模拟实现中,Container 分别默认为 vector< T > 和 list< T >,但事实不然:

在这里插入图片描述
在这里插入图片描述

那么 deque 是什么:

deque(Double ended queue 双端队列)

大体结构示意图:

C++ 栈和队列_第2张图片
优点:

  1. 相比于 vector 扩容代价低(只用对指针数组扩容)
  2. 头插头删、尾插尾删效率高
  3. 支持随机访问(如上图,想访问第10个元素:(10-3) / 10 找到所在行,(10-3) % 10 找到所处位置)

缺点:

  1. 中间插入删除难办,思路:每块空间不一样大,中控中还要存储每块空间的大小,但这么做,随机访问就会略显麻烦
  2. 没有 vector 和 list 的优点极致(尽管如此,在 stack 和 queue 中作为默认底层容器的效率是要优于它们的)

为什么 deque 作为 queue 的默认容器优于 list:deque 具有更加连续的空间,能更好的配合 CPU 的高速缓存

priority_queue 优先级队列

● 优先级高的先出(默认大的数优先级高)
● 底层是个 堆(默认大堆)(把它当做堆来看就行)
● priority_queue< int, vector< int >, greater< int > > pq;
greater< int > 表示小堆(此处用到了 仿函数 )

★仿函数

即类的对象可以像函数一样使用:

struct Less {
	bool operator()(int x, int y) { //重载()
		return x < y;
	}
};
int main() {
	Less lessFunc;
	cout << lessFunc(1, 2) << endl;//把lessFlunc这个对象像函数一样使用
	return 0;
}

将类的对象当函数一样使用,可以解决许多问题,但这最开始是为了解决 函数指针 不好用的问题:

论函数指针到底有多麻烦(看着都费劲):
C++ 栈和队列_第3张图片

C语言中的 qsort(最后要传一个函数指针):

在这里插入图片描述
C++ 中的 sort(最后要传一个仿函数):

在这里插入图片描述

模拟实现优先级队列(以及学习仿函数)

namespace hazb
{
	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 Comapre = less<T>>
	class priority_queue
	{
	public:
		void adjust_up(int child)
		{
			Comapre com;

			int parent = (child - 1) / 2;
			while (child > 0)
			{
				//if (_con[parent] < _con[child])
				if (com(_con[parent], _con[child]))//★使用仿函数优化了上述写法 -- 此后控制大/小堆就无需像C语言一样还要修改自己写的堆里的<>了,直接修改传进来的仿函数就行
				//if (Comapre()(_con[parent], _con[child]))//复习:这是什么写法 —— 匿名对象使用()重载,原来是有名对象
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int parent)
		{
			size_t child = parent * 2 + 1;
			while (child < _con.size())
			{
				Comapre com;

				//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[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> 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, 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*>, PDateGreater> q2;
		//存储数据为日期类的指针,如果仍用此前的 less 和 greater 仿函数,其中比较的就是地址大小 -- 不是我们想要的
		//因而再重新设计一个仿函数
		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;
	}
}

你可能感兴趣的:(c++,开发语言)