C++競プロ学習日記(仮)

( 学習記録であり解説Blogではないです )

Switching Railroad Cars|AOJ|stack

stackに慣れる為に車両入れ替え | Aizu Online Judgeを解きました。

#include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int main()
{
    int N;
    stack<int> st;
    while( cin >> N )
    {
        if( N == 0 ){
            // 0が来たら取り出し
            cout << st.top() << endl;
            st.pop();
            continue;
        }
        // 0以外はstackに積む
        st.emplace( N );
    }
    return 0;
}

TLE本でstackを勉強してから使ってなかったので、使い方の確認をしました。
st.push(N)よりst.emplace(N)(C++11)を使うようにしています。

stackコンテナアダプタ!

参考)stack - cpprefjp C++日本語リファレンス