【Stack & Queue】

1.stack的介绍和使用

⭐跳转链接-> stack的文档介绍

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

简简单单的代码stack的使用

#include<iostream>
using namespace std;
#include<stack>
#include<queue>

void test_stack()
{
	stack<int> s; //定义一个栈
	s.push(1);
	s.push(2);
	s.push(3);
	s.push(4);

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

int main()
{
	test_stack();

	return 0;
}
函数说明接口说明
stack()构造空的栈
empty()检测stack是否为空
size()返回stack中元素的个数
top()返回栈顶元素的引用
push()将元素val压入stack中
pop()将stack中尾部的元素弹出

stack的模拟实现

#include<vector>
namespace bite
{
	template<class T>
 	class stack
 	{
 	public:
 		stack() {}
 		void push(const T& x) {_c.push_back(x);}
 		void pop() {_c.pop_back();}
 		T& top() {return _c.back();}
		const T& top()const {return _c.back();}
		size_t size()const {return _c.size();}
		bool empty()const {return _c.empty();}
	private:
		std::vector<T> _c;
 	};
}

2.queue的介绍和使用

⭐跳转链接->queue的文档介绍

在这里插入图片描述

函数声明接口说明
queue()构造空的队列
empty()检测队列是否为空,是返回true,否则返回false
size()返回队列中有效元素的个数
front()返回队头元素的引用
back()返回队尾元素的引用
push()在队尾将元素val入队列
pop()将队头元素出队列

queue的模拟实现

#include <list>
namespace bite
{
	template<class T>
	class queue
	{
	public:
		queue() {}
		void push(const T& x) {_c.push_back(x);}
		void pop() {_c.pop_front();}
		T& back() {return _c.back();}
		const T& back()const {return _c.back();}
		T& front() {return _c.front();}
		const T& front()const {return _c.front();}
		size_t size()const {return _c.size();}
		bool empty()const {return _c.empty();}
	private:
		std::list<T> _c;
	};
}

3.priorit_queue的介绍和使用

优先级队列默认使用vector作为其底层存储数据的容器,在vector上又使用了堆算法将vector中元素构造成堆的结构,因此priority_queue就是堆,所有需要用到堆的位置,都可以考虑使用priority_queue。

1. 默认情况下priority_queue是大堆。

#include <vector>
#include <queue>
#include <functional> // greater算法的头文件
void TestPriorityQueue()
{
	 // 默认情况下,创建的是大堆,其底层按照小于号比较
	 vector<int> v{3,2,7,6,0,4,1,9,8,5};
	 priority_queue<int> q1;
	 for (auto& e : v)
	 q1.push(e);
	 cout << q1.top() << endl;
	 // 如果要创建小堆,将第三个模板参数换成greater比较方式
	 priority_queue<int, vector<int>, greater<int>> q2(v.begin(), v.end());
	 cout << q2.top() << endl;
}

2. 如果在priority_queue中放自定义类型的数据,用户需要在自定义类型中提供> 或者< 的重载。

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;
};
void TestPriorityQueue()
{
	 // 大堆,需要用户在自定义类型中提供<的重载
	 priority_queue<Date> q1;
	 q1.push(Date(2018, 10, 29));
	 q1.push(Date(2018, 10, 28));
	 q1.push(Date(2018, 10, 30));
	 cout << q1.top() << endl;
	 // 如果要创建小堆,需要用户提供>的重载
	 priority_queue<Date, vector<Date>, greater<Date>> q2;
	 q2.push(Date(2018, 10, 29));
	 q2.push(Date(2018, 10, 28));
	 q2.push(Date(2018, 10, 30));
	 cout << q2.top() << endl;
}

优先队列的模拟实现

#pragma once

#include <iostream>
using namespace std;

#include <vector>
// priority_queue--->堆
namespace bite
{
	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 = std::vector<T>, class Compare = less<T>>
	class priority_queue
	{
	public:
		// 创造空的优先级队列
		priority_queue() : c() {}

		template<class Iterator>
		priority_queue(Iterator first, Iterator last)
			: c(first, last)
		{
			// 将c中的元素调整成堆的结构
			int count = c.size();
			int root = ((count - 2) >> 1);
			for (; root >= 0; root--)
				AdjustDown(root);
		}

		void push(const T& data)
		{
			c.push_back(data);
			AdjustUP(c.size() - 1);
		}

		void pop()
		{
			if (empty())
				return;

			swap(c.front(), c.back());
			c.pop_back();
			AdjustDown(0);
		}

		size_t size()const
		{
			return c.size();
		}

		bool empty()const
		{
			return c.empty();
		}

		// 堆顶元素不允许修改,因为:堆顶元素修改可以会破坏堆的特性
		const T& top()const
		{
			return c.front();
		}
	private:
		// 向上调整
		void AdjustUP(int child)
		{
			int parent = ((child - 1) >> 1);
			while (child)
			{
				if (Compare()(c[parent], c[child]))
				{
					swap(c[child], c[parent]);
					child = parent;
					parent = ((child - 1) >> 1);
				}
				else
				{
					return;
				}
			}
		}

		// 向下调整
		void AdjustDown(int parent)
		{
			size_t child = parent * 2 + 1;
			while (child < c.size())
			{
				// 找以parent为根的较大的孩子
				if (child + 1 < c.size() && Compare()(c[child], c[child + 1]))
					child += 1;

				// 检测双亲是否满足情况
				if (Compare()(c[parent], c[child]))
				{
					swap(c[child], c[parent]);
					parent = child;
					child = parent * 2 + 1;
				}
				else
					return;
			}
		}
	private:
		Container c;
	};
}

void TestQueuePriority()
{
	bite::priority_queue<int> q1;
	q1.push(5);
	q1.push(1);
	q1.push(4);
	q1.push(2);
	q1.push(3);
	q1.push(6);
	cout << q1.top() << endl;

	q1.pop();
	q1.pop();
	cout << q1.top() << endl;

	vector<int> v{ 5,1,4,2,3,6 };
	bite::priority_queue<int, vector<int>, bite::greater<int>> q2(v.begin(), v.end());
	cout << q2.top() << endl;

	q2.pop();
	q2.pop();
	cout << q2.top() << endl;
}

4.容器适配器

  • 🔺什么是适配器呢?
    适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口。

  • 25
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值