I\'m trying to read in data and solve simple problem, data :
3 - number of lines to read in
1 1
2 2 2
3 4
after each line is
C++ gives you many tools:
string
is a class managing the lifetime of character strings.getline
puts a line from a stream (up to the next newline) into a string
.istringstream
makes an istream
out of a string
istream_iterator
iterates over an istream
, extracting the given type, breaking on whitespace.accumulate
takes iterators delimiting a range and adds together the values:Altogether:
string line;
while (getline(cin, line)) {
istringstream in(line);
istream_iterator begin(in), end;
int sum = accumulate(begin, end, 0);
cout << sum << '\n';
}