<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; }