Assuming you already read the starter N, there is a good trick using istream_iterator:
std::vector<int> nums;
nums.reserve(N);
std::copy(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>(),
std::back_inserter(nums));
The object back_inserterturns into an iterator, which adds elements to the vector at the end. Iterator streams can be parameterized by the type of read elements, and, if the parameter is not specified, signals the end of the input.
source
share