STL-stack(栈)

来源:互联网 发布:重装开票软件 编辑:程序博客网 时间:2024/06/06 12:30

stack是一种自适应容器,使用方法详见程序。
定义:

template<class elementType, class Container=deque<Type>> class stack;

可使用push()在栈顶加入元素,可使用pop()在栈顶删除元素;可使用top()取栈顶元素;可使用size()计算栈的大小;可使用empty()判断栈是否为空。

#include<stack>#include<vector>#include<iostream>#include<stdlib.h>using namespace std;int main(){    stack<int> stackints;    //stack<double> stackdoubles;    //stack<int, vector<double>> stackdoublevectors;    //stack<int> stackintcopy(stackints);    stackints.push(25);    stackints.push(10);    stackints.push(-1);    stackints.push(5);    //    cout << "stackints contains" << stackints.size() << "elements" << endl;    while (stackints.size() != 0)    {        cout << " popint the topmost elements:" << stackints.top() << endl;        stackints.pop();    }    if (stackints.empty())        cout << "pop all elements" << endl;    system("pause");    return 0;}
0 0