How do I use an iterator on an ifstream twice in C++? -
i'm new c++ , i'm confused using iterators ifstream. in following code have ifstream variable called datafile.
in code first iterate through file once count how many characters has (is there more efficient way this?). create matrix of size, , iterate through again fill matrix.
the problem iterator refuses iterate second time around, , not anything. tried resetting ifstream beginning using datafile.clear(), didn't work, because have deep misunderstanding iterators. me please?
typedef istreambuf_iterator<char> dataiterator; (dataiterator counter(datafile), end; counter != end; ++counter, ++numcodons) {} // finds file size in characters. matrixxd ymatrix = matrixxd::constant(3, numcodons, 0); datafile.clear(); // resets ifstream used again. (dataiterator counter(datafile), end; counter != end; ++counter) {...}
istreambuf_iterator
input iterator once has been incremented, copies of previous value may invalidated, not forward iterator guarantees validity when used in multipass algorithms. more category of iterators, see here.
Comments
Post a Comment