stackコンテナ / LIFO (後入れ先出し)タイプのデータ格納方式

<stack>をインクルードする。

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

int main() {
    stack <int> st; // intを格納するスタックを変数stとして宣言する。

    for (int i = 0; i < 5; i++) {
        st.push(i); // .push()でスタックに値を格納する。
    }

    while (!st.empty()) {
        cout << st.top() << ","; // スタックに最後に格納された値は.top()で参照される。
        st.pop();                // スタックに最後に格納された値は.pop()で削除される。
    }

    return 0;
}

f:id:ti-nspire:20190620073917p:plain