zoukankan      html  css  js  c++  java
  • STL之stack

    描述

    使用STL中的stack,完成入栈、出栈、栈清空等基本操作。

    部分代码已经给出,请补充完整,提交时请勿包含已经给出的代码。

     

    int main()
    {
        stack<int> st;
        int n;
        cin>>n;
        while(n--)
        {
            Op(st);
        }
        while(!st.empty())
        {
            cout<<st.top()<<endl;
            st.pop();
        }
        return 0;
    }

    输入

    输入数据第一行为整数n,接下来有n个命令,其中命令格式:

    (1)压栈:push x

    (2)出栈:pop

    (3)清空:clear

    如果部分操作无效,该操作不执行。

    输出

    执行一系列操作后,输出从栈顶到栈底的所有元素值,每行一个。

    样例输入

     5
    push 1
    push 2
    pop
    push 3
    push 4

    样例输出

    4
    3
    1

    #include <iostream>
    #include <string>
    #include <stack>
    using namespace std;
    void Op(stack<int> &st)
    {
        string ss;
        cin>>ss;
        if(ss=="push")
        {
            int n;
            cin>>n;
            st.push(n);
        }
        else if(ss=="pop")
        {
            if(!st.empty())
            {
                st.pop();
            }
        }
        else if(ss=="clear")
        {
            while(!st.empty())
            {
                st.pop();
            }
        }
        else
        {
            if(!st.empty())
            {
                cout<<st.top()<<endl;
            }
        }
    }
    int main()
    {
        stack<int> st;
        int n;
        cin>>n;
        while(n--)
        {
            Op(st);
        }
        while(!st.empty())
        {
            cout<<st.top()<<endl;
            st.pop();
        }
        return 0;
    }

     

     

  • 相关阅读:
    Rolling File Appender使用示例
    log4net生成dll文件
    看涨期权(call options)
    log4net file Appender使用示例
    log4net不能记录日志,IsErrorEnabled值为false
    C#委托
    打印事件处理顺序
    Zigbee、WiFi和433MHz无线技术
    log4net Tutorial
    安装服务出现异常
  • 原文地址:https://www.cnblogs.com/andrew3/p/8722148.html
Copyright © 2011-2022 走看看