【C++】stack和queue使用以及模拟实现(介绍deque适配器)

一、stack介绍

c++中的栈,LIFO后进先出原则
image-20220407172348701

注意这里栈和队列不再是容器(vector,string),而是容器适配器

是由容器转换过来的,可以看出默认的转换是deque


重要的几个成员函数,和当初模拟实现栈一样

image-20220401145734170


二、使用stack

1. 先进后出的体现

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stack>
using namespace std;

void test1()
{
	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()
{
	test1();
	return 0;
}

测试结果:

image-20220401150732624

因为栈是先进后出,所以不支持迭代器(结构的问题)


三、queue介绍

c++中的队列包含队列和优先级队列(后话)

image-20220401151213330


本次介绍:

它也是容器适配器,FIFO先进先出原则

image-20220401151003537

重要成员

image-20220401151536625


四、queue的使用

1. 先进先出

void test2()
{
	queue<int> q;
	q.push(1);
	q.push(2);
	q.push(3);
	q.push(4);

	while (!q.empty())
	{
		cout << q.front();
		q.pop();
	}
	cout << endl;

}

测试结果:

image-20220401151906730


五、模拟实现

1. 模拟实现stack

	template<class T, class Container>
	class stack
	{
	public:
		bool empty()
		{
			return _con.empty();
		}

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

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

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

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

测试:

	void stack_test()
	{
		stack<int, std::vector<int>> st;

		st.push(1);
		st.push(2);
		st.push(3);
		st.push(4);

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

image-20220406141013287


2. 模拟实现queue

	template<class T, class Container = deque<T>>
	class queue
	{
	public:
		bool empty()
		{
			return _con.empty();
		}

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

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

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

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

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

测试:

	void queue_test()
	{
		queue<int, std::list<int>> q;

		q.push(1);
		q.push(2);
		q.push(3);
		q.push(4);

		while (!q.empty())
		{
			cout << q.front() << endl;
			q.pop();
		}
		cout << endl;
	}
}

image-20220406142551564


六、deque

image-20220406142801092

设计出来就是想要融合vector和list的优缺点


image-20220407165115685

  • 与vector比较,deque的优势是:头部插入和删除时,不需要搬移元素,效率特别高,而且在扩容时,也不 需要搬移大量的元素,因此其效率是必vector高的。
  • 与list比较,其底层是连续空间,空间利用率比较高,不需要存储额外字段。
  • 但是,deque有一个致命缺陷:不适合遍历,因为在遍历时,deque的迭代器要频繁的去检测其是否移动到,某段小空间的边界,导致效率低下,而序列式场景中,可能需要经常遍历,因此在实际中,需要线性结构时,大多数情况下优先考虑vector和list,deque的应用并不多,而目前能看到的一个应用就是,STL用其作为stack和queue的底层数据结构。

题目

在这里插入图片描述

class MinStack {
public:
    //自定义类型调用自己的构造函数
    //不需要写
    MinStack() {}
    
    void push(int val) {
        _st.push(val);

        //_min为空直接插入,不为拿pop去和插入值比较,val更小或者相等插val
        if(_min.empty() || _min.top() >= val)
        {
            _min.push(val);
        }
    }
    
    void pop() {
        if(_min.top() == _st.top())
        {
            _min.pop();
        }
        _st.pop();
    }
    
    int top() {
        return _st.top();
    }
    
    int getMin() {
        return _min.top();
    }

    stack<int> _st;
    stack<int> _min;
};

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凛音Rinne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值