[Baekjoon] - 스택

문제

백준 스택에 관한 문제이다.

stl 공부겸 c++ 알아가기

코드

#include <iostream>
#include <string>
#include <stack>
using namespace std;

int main()
{
    int n;
    string oper;
    int value;
    stack<int> st;

    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> oper;

        if (oper == "push"){
            cin >> value;
            st.push(value);
        }

        else if (oper == "pop"){
            if (st.size() == 0)
                cout << "-1" << endl;
            else{
                cout << st.top() << endl;
                st.pop();
            }
        }

        else if (oper == "size"){
            cout << st.size() << endl;
        }

        else if (oper == "empty"){
            if (st.size() == 0)
                cout << "1" << endl;
            else
                cout << "0" << endl;
        }
        else if (oper == "top"){
            if (st.size() == 0)
                cout << "-1" <<endl;
            else
                cout << st.top() << endl;
        }
    }
}

요약

  • 문제에 조건이 안들어간 부분이 있다. 프로그래머스가 좋은 편.
  • java에서는 pop() 리턴 값이 있는데 stl에는 없는 것 같다.