C++标准模板库STL--stack、queue、priority

C++标准模板库STL--stack、queue、priority,第1张

●🧑个人主页:你帅你先说.
●📃欢迎点赞👍关注💡收藏💖
●📖既选择了远方,便只顾风雨兼程。



●🤟欢迎大家有问题随时私信我!
●🧐版权:本文由[你帅你先说.]原创,CSDN首发,侵权必究。


📌📌📌为您导航📌📌📌
  • 🐕1.stack和queue的使用
    • 🦮1.1 stack的基本 *** 作
    • 🦮1.2 queue的基本 *** 作
  • 🐅2.适配器
    • 🐆2.1什么是适配器
    • 🐆2.2STL标准库中stack和queue的底层结构
    • 🐆2.3deque的优点
  • 🐎3.stack和queue模拟实现
  • 🐇4.priority_queue
    • 🐿️4.1优先队列介绍
    • 🐿️4.2模拟实现

🐕1.stack和queue的使用

有了前面STL的基础,相信这些函数的基本 *** 作对大家来说so easy,所以我们简单来介绍。


🦮1.1 stack的基本 *** 作

1.stack是一种容器适配器,专门用在具有后进先出 *** 作的上下文环境中,其删除只能从容器的一端进行 元素的插入与提取 *** 作。



2.stack是作为容器适配器被实现的,容器适配器即是对特定类封装作为其底层的容器,并提供一组特定 的成员函数来访问其元素,将特定类作为其底层的,元素特定容器的尾部(即栈顶)被压入和d出。



3.stack的底层容器可以是任何标准的容器类模板或者一些其他特定的容器类,这些容器类应该支持以下 *** 作:

  • empty:判空 *** 作
  • back:获取尾部元素 *** 作
  • push_back:尾部插入元素 *** 作
  • pop_back:尾部删除元素 *** 作

4.标准容器vector、deque、list均符合这些需求,默认情况下,如果没有为>stack指定特定的底层容器, 默认情况下使用deque。


#include
#include
using namespace std;
int main()
{
	//定义栈
	stack<int> st;

	//进栈
	st.push(1);
	st.push(2);
	st.push(3);
	st.push(4);
	st.push(5);

	//出栈
	st.pop();
	st.pop();
	st.pop();

	//取栈顶
	st.top();

	//判断栈是否为空
	st.empty();

	return 0;
}
🦮1.2 queue的基本 *** 作

1.队列是一种容器适配器,专门用于在FIFO上下文(先进先出)中 *** 作,其中从容器一端插入元素,另一端提取元素。



2.队列作为容器适配器实现,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的 成员函数来访问其元素。


元素从队尾入队列,从队头出队列。



3.底层容器可以是标准容器类模板之一,也可以是其他专门设计的容器类。


该底层容器应至少支持以下 *** 作:

  • empty:检测队列是否为空
  • size:返回队列中有效元素的个数
  • front:返回队头元素的引用
  • back:返回队尾元素的引用
  • push_back:在队列尾部入队列
  • pop_front:在队列头部出队列

4.标准容器类deque和list满足了这些要求。


默认情况下,如果没有为queue实例化指定容器类,则使用标 准容器deque。


#include
#include
using namespace std;
int main()
{
	//定义队列
	queue<int> qu;

	//进队
	qu.push(1);
	qu.push(2);
	qu.push(3);
	qu.push(4);
	qu.push(5);

	//出队
	qu.pop();
	qu.pop();
	qu.pop();

	//取队头
	qu.front();

	//取队尾
	qu.back();

	//判断队是否为空
	qu.empty();

	//队的大小
	qu.size();
	
	return 0;
}
🐅2.适配器 🐆2.1什么是适配器

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



🐆2.2STL标准库中stack和queue的底层结构

虽然stack和queue中也可以存放元素,但在STL中并没有将其划分在容器的行列,而是将其称为容器适配器,这是因为stack和队列只是对其他容器的接口进行了包装,STL中stack和queue默认使用deque





deque这里不详细介绍,你只要知道它是一个双端队列就可以了。


🐆2.3deque的优点

我们知道,vector的缺点是扩容代价大、不适合头插头删,list的缺点是不支持随机访问。


而deque完全克服了这些缺点,首先deque是双端队列,那它头插头删尾插尾删的效率肯定是极致的,其次deque的扩容代价并不大,不需要拷贝数据,CPU高速缓存命中高,不会频繁申请释放空间,所以它作为默认容器再合适不过了。


🐎3.stack和queue模拟实现

stack模拟实现

#include
#include
using namespace std;
namespace ljt
{
	template<class T, class Container = deque<T>>
	class stack
	{
	public:
		bool empty() const
		{
			return _con.empty();
		}

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

		const T& top() const
		{
			return _con.back();
		}

		void push(const T& x)
		{
			_con.push_back(x);
		}

		void pop()
		{
			_con.pop_back();
		}
	private:
		Container _con;
	};
}

大家会发现,在这一块的模拟实现成本已经比之前低很多了,本质就是复用。


这就是适配器的优势。


queue的模拟实现

#include
#include
using namespace std;
namespace ljt
{
	template<class T, class Container = deque<T>>
	class queue
	{
	public:
		bool empty() const
		{
			return _con.empty();
		}

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

		const T& front() const
		{
			return _con.front();
		}

		const T& back() const
		{
			return _con.back();
		}

		void push(const T& x)
		{
			_con.push_back(x);
		}

		void pop()
		{
			_con.pop_front();
		}
	private:
		Container _con;
	};
}
🐇4.priority_queue 🐿️4.1优先队列介绍

1.优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。



2.此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元素)。



3.优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。


元素从特定容器的“尾部”d出,其称为优先队列的顶部。



4.底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。


容器应该可以通过随机访问迭代器访问,并支持以下 *** 作:

  • empty():检测容器是否为空
  • size():返回容器中有效元素个数
  • front():返回容器中第一个元素的引用
  • push_back():在容器尾部插入元素
  • empty( )检测优先级队列是否为空,是返回true,否则返回false
  • top( ) 返回优先级队列中最大(最小元素),即堆顶元素 push(x) 在优先级队列中插入元素x
  • pop()删除优先级队列中最大(最小)元素,即堆顶元素
  • pop_back():删除容器尾部元素

5.标准容器类vector和deque满足这些需求。


默认情况下,如果没有为特定的priority_queue类实例化指定容器类,则使用vector。



6.需要支持随机访问迭代器,以便始终在内部保持堆结构。


容器适配器通过在需要时自动调用算法函数make_heap、push_heap和pop_heap来自动完成此 *** 作。


🐿️4.2模拟实现
namespace ljt
{
	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>>
	class priority_queue
	{
	private:
		void adjust_up(size_t child)
		{
			Compare com;
			size_t parent = (child - 1) / 2;
			while (child > 0)
			{
				if (com(_con[parent], _con[child]))
				{
					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;
				}
			}
		}

	public:
		priority_queue()
		{}

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
			:_con(first, last)
		{
			// 建堆
			for (int i = (_con.size() - 1 - 1) / 2; i >= 0; --i)
			{
				adjust_down(i);
			}
		}

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

		void pop()
		{
			//assert(!_con.empty());
			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;
	};
}

这里的模拟实现涉及到堆的相关实现,忘记的同学可以去看一下这篇 数据结构—二叉树、堆
喜欢这篇文章的可以给个一键三连 点赞👍关注💡收藏💖

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/562673.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-02
下一篇 2022-04-02

发表评论

登录后才能评论

评论列表(0条)

保存