Search code examples
c++iteratorfstreamistream

How to do is >> std::skipws >> through multiple indices of an array?


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.


Solution

  • How to write this generically for any value of SIZE.

    There are control structures for repeating an operation a variable number of times: loops.

    For example:

    is >> std::skipws;
    for(auto& el : a) {
        is >> el;
    }