美文网首页
C++ STL stack 栈

C++ STL stack 栈

作者: 小柳学渣 | 来源:发表于2019-01-27 00:16 被阅读0次
#include <iostream>
#include <stack>
using namespace std; 
int main()
{
    std::ios::sync_with_stdio(false);
    //cin.tie(NULL); 
    stack<int> s;
    int choice,e;
    while(true)
    {
        cout<<"1.判断栈是否为空\n"
              "2.栈中元素个数\n"
              "3.删除栈顶元素\n"
              "4.输出栈顶元素\n"
              "5.插入元素\n";
        cin>>choice;
        switch(choice)
        {
            case 1:cout<<(s.empty()?"空\n":"不空\n");break;
            case 2:cout<<"元素个数为:"<<s.size()<<"\n";break;
            case 3:if(s.empty())cout<<"栈为空\n";else{s.pop();cout<<"删除成功\n";}break; 
            case 4:s.empty()?cout<<"栈为空\n":cout<<"栈顶元素为:"<<s.top()<<"\n";break; 
            case 5:cout<<"请输入要插入的元素:";cin>>e;s.push(e);cout<<"插入成功\n";break;
            default:cout<<"输入错误,请重新输入!\n";
        } 
    }
    return 0;
} 
/************************************************
*                 小柳学渣
*             2019/1/22  19:34 
************************************************/ 

相关文章

网友评论

      本文标题:C++ STL stack 栈

      本文链接:https://www.haomeiwen.com/subject/piowjqtx.html