赞
踩
根据stack的文档介绍可以知道,stack是一种容器适配器,专门设计用于在后进先出上下文(后进先出)中运行,其中元素仅从容器的一端插入和提取。
stack作为容器适配器实现,容器适配器是使用特定容器类的封装对象作为其底层容器的类,提供一组特定的成员函数来访问其元素。元素从特定容器的“背面”推出/弹出,这被称为stack的顶部(即栈顶)。
stack的底层容器可以是任何标准容器类模板,也可以是一些其他专门设计的容器类。容器应支持以下操作:
标准容器类 vector、deque、 list ,均满足这些要求。默认情况下,如果未为特定类实例化指定容器类,则使用标准容器deque作为底层容器。
以上是stack的成员函数,用蓝色框框框起来的几个成员函数接口是需要重点掌握的
简单说明:
1.stack()是构造一个空的stack
2.empty()是用于判断stack是否为空
3.size()是用来返回stack中元素的个数
4.top()适用于返回栈顶元素的引用
5.push()将元素压入stack中
6.pop()将stack中尾部的元素弹出
下面对这几个接口进行使用,以下是简单的测试代码:
- #include<iostream>
- #include<stack>
- #include<queue>
- using namespace std;
-
- void test_stack1()
- {
- stack<int> st;
- st.push(1);
- st.push(2);
- st.push(3);
- st.push(4);
-
- while (!st.empty())
- {
- cout << st.top() <<" ";
- st.pop();
- }
- }
-
- int main()
- {
- test_stack1();
- return 0;
- }
运行结果如下:
当然也不一定是入1,2,3,4出的序列就一定是4,3,2,1。也可以边入边出
- #define _CRT_SECURE_NO_WARNINGS 1
-
- #include<iostream>
- #include<stack>
- #include<queue>
- using namespace std;
-
- void test_stack1()
- {
- stack<int> st;
- st.push(1);
- st.push(2);
- cout << st.top() << " ";
- st.pop();
- st.push(3);
- st.push(4);
-
- while (!st.empty())
- {
- cout << st.top() <<" ";
- st.pop();
- }
- }
-
- int main()
- {
- test_stack1();
- return 0;
- }
运行结果如下:
学会了如何使用之后我们可以对该oj题进行挑战
OJ题参考:
- class MyQueue {
- public:
- MyQueue() {
-
- }
-
- void push(int x) {
- st1.push(x);
- }
-
- int pop() {
- if(st2.empty())
- {
- while(!st1.empty())
- {
- st2.push(st1.top());
- st1.pop();
- }
- }
-
- int top = st2.top();
- st2.pop();
- return top;
- }
-
- int peek() {
- if(st2.empty())
- {
- while(!st1.empty())
- {
- st2.push(st1.top());
- st1.pop();
- }
- }
- return st2.top();
- }
-
- bool empty() {
- return st1.empty()&&st2.empty();
- }
-
- private:
- stack<int> st1;//用来实现从队尾入队列
- stack<int> st2;//用来实现从队头出队列
- };
参考实现:
思路是用两个栈来实现,一个用来放栈中的数据,一个用来存放该栈数据中的最小值,如果初始时st为空,那么push的就是目前栈中的最小值,然后把这个最小值在Minst这个栈中push进去,如果st中push的值小于Minst目前的最小值则要继续push,如果与最小值相等,为了pop之后不受影响,则也要在Minst中继续push该相等的最小值,这样就能实现一直能够获取栈中的最小值了。
- class MinStack {
- public:
- MinStack() {
-
- }
-
- void push(int val) {
- if(st.empty()||Minst.top()>=val)
- {
- Minst.push(val);
- }
- st.push(val);
-
- }
-
- void pop() {
- if(st.top()==Minst.top())
- {
- Minst.pop();
- }
-
- st.pop();
- }
-
- int top() {
- return st.top();
- }
-
- int getMin() {
- return Minst.top();
- }
-
- stack<int> st;
- stack<int> Minst;
- };
解题思路:
代码参考实现:
- class Solution {
- public:
- /**
- * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
- *
- *
- * @param pushV int整型vector
- * @param popV int整型vector
- * @return bool布尔型
- */
- bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {
- // write code here
-
- stack<int> st;
- int pushi= 0,popi = 0;
- while(pushi<pushV.size())
- {
- st.push(pushV[pushi]);
- while(!st.empty()&&st.top()==popV[popi])
- {
- st.pop();
- popi++;
- }
- pushi++;
- }
-
- return st.empty();
- }
- };
- #include<deque>
- namespace mystack
- {
- template<class T, class Container=deque<T>>
- class stack
- {
- public:
-
- void push(const T& x)
- {
- _con.push_back(x);
- }
-
- void pop()
- {
- _con.pop_back();
- }
-
- const T& top()
- {
- return _con.back();
- }
-
- size_t size()
- {
- return _con.size();
- }
-
- bool empty()
- {
- return _con.empty();
- }
-
- private:
- Container _con;
- };
- }
模拟实现功能测试代码:
- void test_stack2()
- {
- mystack::stack<int> st;
- st.push(1);
- st.push(2);
- st.push(3);
- st.push(4);
-
- while (!st.empty())
- {
- cout << st.top() << " ";
- st.pop();
- }
- }
运行结果截图:
根据queue的文档介绍可以知道,
队列 是一种容器适配器,专门设计用于在 FIFO 上下文(先进先出)中运行,其中元素插入容器的一端并从另一端提取。
队列作为容器适配器实现,容器适配器是使用特定容器类的封装对象作为其基础容器的类,提供一组特定的成员函数来访问其元素。元素被推入特定容器的“背面”(即队尾),并从其“正面”(即队头)弹出。
基础容器可以是标准容器类模板之一,也可以是其他一些专门设计的容器类。此基础容器应至少支持以下操作:
标准容器类,并满足这些要求。默认情况下,如果未为特定类实例化指定容器类,则使用标准容器。deque list queue deque
以上是queue的成员函数接口,下面对这几个接口进行简单介绍:
1.queue()是构造一个空的queue
2.empty()是用于判断queue是否为空,是的话返回true,否则返回false.
3.size()是用来返回queue中元素的个数
4.top()适用于返回队头元素的引用
5.push()在队尾将元素入队列
6.pop()将队头元素出队列
下面我们对这些成员函数接口进行使用
- #include<iostream>
- #include<queue>
- using namespace std;
-
-
- void test_queue1()
- {
- queue<int> q;
-
- q.push(1);
- q.push(2);
- q.push(3);
- q.push(4);
-
- while (!q.empty())
- {
- cout << q.front() << " ";
- q.pop();
- }
- }
-
- int main()
- {
- test_queue1();
- return 0;
- }
运行结果如下:
熟悉了这些使用之后我们可以挑战一些该oj题目:
-
- namespace myqueue
- {
- template<class T,class Container=deque<T>>
- class queue
- {
- public:
- void push(const T& x)
- {
- _con.push_back(x);
- }
-
- void pop()
- {
- _con.pop_front();
- }
-
- const T& front()
- {
- return _con.front();
- }
-
- const T& back()
- {
- return _con.back();
- }
-
- size_t size()
- {
- return _con.size();
- }
-
- bool empty()
- {
- return _con.empty();
- }
-
- private:
- Container _con;
- };
- }
代码测试:
- void test_queue2()
- {
- myqueue::queue<int> q;
- q.push(4);
- q.push(3);
- q.push(1);
- q.push(0);
- q.push(2);
- q.push(5);
-
- while (!q.empty())
- {
- cout << q.front() << " ";
- q.pop();
- }
- cout << endl;
- }
运行结果:
1. 优先队列是一种容器适配器,根据严格的弱排序标准,它的第一个元素总是它所包含的元素中最大的。
2. 此上下文类似于堆,在堆中可以随时插入元素,并且只能检索最大堆元素(优先队列中位于顶部的元
素)。
3. 优先队列被实现为容器适配器,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特
定的成员函数来访问其元素。元素从特定容器的“尾部”弹出,其称为优先队列的顶部。
4. 底层容器可以是任何标准容器类模板,也可以是其他特定设计的容器类。容器应该可以通过随机访问迭
代器访问,并支持以下操作:
empty():检测容器是否为空
size():返回容器中有效元素个数
front():返回容器中第一个元素的引用
push_back():在容器尾部插入元素
pop_back(): 删除容器尾部元素
5. 标准容器类vector和deque满足这些需求。默认情况下,如果没有为特定的priority_queue类实例化指
定容器类,则使用vector。
6. 需要支持随机访问迭代器,以便始终在内部保持堆结构。容器适配器通过在需要时自动调用算法函数
make_heap、push_heap和pop_heap来自动完成此操作。
以上是priority_queue的成员函数接口,下面对常用的接口进行简单说明
priority_queue()/priority_queue(first,last) 构造一个空的优先级队列
empty( ) 检测优先级队列是否为空,是返回true,否则返回false
top( ) 返回优先级队列中最大(最小元素),即堆顶元素
push(x) 在优先级队列中插入元素x
pop() 删除优先级队列中最大(最小)元素,即堆顶元素
- void test_mypriority_queue()
- {
- mypriority_queue::priority_queue<int> pq;
- pq.push(4);
- pq.push(3);
- pq.push(1);
- pq.push(0);
- pq.push(2);
- pq.push(5);
-
- while (!pq.empty())
- {
- cout << pq.top() << " ";
- pq.pop();
- }
- cout << endl;
- }
运行结果如下:
在OJ中的使用:
- #pragma once
-
- #include <iostream>
- using namespace std;
-
- #include <vector>
- // priority_queue--->堆
- namespace mypriority_queue
- {
- 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()
- {
- mypriority_queue::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 };
- mypriority_queue::priority_queue<int, vector<int>, mypriority_queue::greater<int>> q2(v.begin(), v.end());
- cout << q2.top() << endl;
-
- q2.pop();
- q2.pop();
- cout << q2.top() << endl;
- }
适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口。
虽然stack和queue中也可以存放元素,但在STL中并没有将其划分在容器的行列,而是将其称为容器适配器,这是因为stack和队列只是对其他容器的接口进行了包装,STL中stack和queue默认使用deque,比如:
deque(双端队列):是一种双开口的"连续"空间的数据结构,双开口的含义是:可以在头尾两端进行插入和删除操作,且时间复杂度为O(1),与vector比较,头插效率高,不需要搬移元素;与list比较,空间利用率比较高。
deque并不是真正连续的空间,而是由一段段连续的小空间拼接而成的,实际deque类似于一个动态的二维数组。
双端队列底层是一段假象的连续空间,实际是分段连续的,为了维护其“整体连续”以及随机访问的假象,落在了deque的迭代器身上,因此deque的迭代器设计就比较复杂。
deque的缺陷
与vector比较,deque的优势是:头部插入和删除时,不需要搬移元素,效率特别高,而且在扩容时,也不需要搬移大量的元素,因此其效率是必vector高的。
与list比较,其底层是连续空间,空间利用率比较高,不需要存储额外字段。
但是,deque有一个致命缺陷:不适合遍历,因为在遍历时,deque的迭代器要频繁的去检测其是否移动到某段小空间的边界,导致效率低下,而序列式场景中,可能需要经常遍历,因此在实际中,需要线性结构时,大多数情况下优先考虑vector和list,deque的应用并不多,而目前能看到的一个应用就是,STL用其作为stack和queue的底层数据结构
stack是一种后进先出的特殊线性数据结构,因此只要具有push_back()和pop_back()操作的线性结构,都可以作为stack的底层容器,比如vector和list都可以;queue是先进先出的特殊线性数据结构,只要具有push_back和pop_front操作的线性结构,都可以作为queue的底层容器,比如list。但是STL中对stack和queue默认选择deque作为其底层容器,主要是因为:
1. stack和queue不需要遍历(因此stack和queue没有迭代器),只需要在固定的一端或者两端进行操作。
2. 在stack中元素增长时,deque比vector的效率高(扩容时不需要搬移大量数据);queue中的元素增长时,deque不仅效率高,而且内存使用率高。
结合了deque的优点,而完美的避开了其缺陷。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。