Q:

Reading a list of integer in a single input line in C++

I'm trying to read multiple integers from a single input line into an array
eg. Input: 100 200 300 400, so the array is: a[0] = 100, a[1] = 200, a[2] = 300, a[3] = 400
The thing is, the number of integers are unknown, so the size of the array is unknown.

A:

You should use a container that automatically resizes itself, such as std::vector.
For example, something like this:
#include <string>
#include <iostream>
#include <sstream>
#include <utility>
#include <iterator>

std::string line;
getline(instream, line);
std::istringstream this_line(line);
std::istream_iterator<int> begin(this_line), end;
std::vector<int> values(begin, end);

A:

You could use std::vector for this:
std::vector<int> myVector;

std::string line;
std::getline(std::cin, line);
std::istringstream os(line);

int i;
while(os >> i)
    myVector.push_back(i);

This code requires following includes: <iostream>, <string>, <sstream> and <vector>.

