D - Delimiter Editorial by seekworser


C++ の bool(std::cin) は、読み込みの失敗時に false 、そうではない場合は true として評価されます。したがって、std::cin 自体を while 文の引数にとることで、入力の末尾に達するまで入力を受け取ることが可能です。

#include <bits/stdc++.h>
using namespace std;
int main() {
    int x;
    vector<int> ans;
    while (cin >> x) ans.push_back(x);
    reverse(ans.begin(), ans.end());
    for (auto x : ans) cout << x << '\n';
}

posted:
last update: