stack and stack array

declare a stack

stack<int> s1;
   stack<string> s2;
   stack中的操作
    stack<int> s;
    s.push(x)      无返回值,将元素x压栈
    s.pop();       退栈,无返回值
    s.top();        取栈顶元素,返回栈顶元素
    s.empty();     判断栈是否为空,如果是空,返回1,否则返回0
    s.size();      返回栈中元素的个数
   没有清空栈的操作函数,但是可以间接地实现清空栈,
   while(!s.empty())
   {    
          s.pop();
   }

declare a stack array

#include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
    stack<int>a[10];  //声明一个栈的数组 
    for(int i=0;i<10;i++) //数组栈的清空 
    {
        while(!a[i].empty())
        {
            a[i].pop();
        }
    } 
    int x=1;
    a[1].push(x);     // 无返回值,将元素x压栈
    a[1].pop();      // 退栈,无返回值
    a[1].top();       // 取栈顶元素,返回栈顶元素
    a[1].empty();     //判断栈是否为空,如果是空,返回1,否则返回0
    a[1].size();     // 返回栈中元素的个数

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325539805&siteId=291194637