Let's say you have std::array<int, SIZE> a
, and you have saved each element of a
into a file in one line separated by a space. Then you want to read them with a std:istream& is
via:
is >> std::skipws >> a[0] >> a[1] >> a[2] >> ... >> a[SIZE-1];
How to write this generically for any value of SIZE
. Even though there are other easy ways of doing this, I'm curious how it is done with this particular method.
CodePudding user response:
How to write this generically for any value of SIZE.
There are a control structures for repeation an operation for variable number of times: loops. Example:
is >> std::skipws;
for(auto& el : a) {
is >> el;
}