1、STACK
相对来说,STACK是一个比较简单的容器,它的使用也比较简单。
STACK是LIFO容器,就是后进先出。最后添加进去的元素,第一个取出来。
2、STACK常用方法
template stack<class T,class Container = deque<T>> class stack
其中T 为类型,container为存储和访问元素的类型
empty();//判断是否为空
push(class T);//栈顶压入一元素
pop();//弹出栈顶元素
top();//返回栈顶元素
size();//返回栈中元素个数
3 、示例代码
#include "stdafx.h"
#include <stack>
#include <vector>
#include <deque>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
stack<int> stackWithDefaultContainer;
stack<int,vector<int>> stackWithVectorContainer;
stack<int,deque<int>> stackWithDequeContainer;
stackWithDequeContainer.push(10);
stackWithDequeContainer.push(11);
stackWithVectorContainer.push(0);
stackWithVectorContainer.push(1);
cout<<"QueueStackSize:"<<stackWithDequeContainer.size()<<endl;
cout<<"VectorStackSize"<<stackWithVectorContainer.size()<<endl;
for (int index = 0;index<10;index++)
{
stackWithDefaultContainer.push(index);
}
cout<<"Start to pop:"<<endl;
while (!stackWithDefaultContainer.empty())
{
int iOnePopData = stackWithDefaultContainer.top();
cout<<iOnePopData<<" ";
stackWithDefaultContainer.pop();
}
cout<<endl;
cout<<"End"<<endl;
}
运行结果
<img src="https://img-blog.csdn.net/20140927125619609?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSGVsbG9fSHdj/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />