I am trying to conert a vector into a stream and back.
While the first part works without issues, the last line of the following code put std::ios_base is an error state. Have you got any idea why it is so?
Apparently myVecOut is equal to myVec after the code executes….
std::vector<double> myVec( 3 );
std::stringstream temp;
std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<double>(temp, " "));
std::cout << temp.str() << std::endl;
std::vector<double> myVecOut;
std::copy(std::istream_iterator<double>(temp), std::istream_iterator<double>(), std::back_inserter(myVecOut));
It’s set into
failstate, becausecopydoes not know how many items to read. It reads as long as the stream is in anon-.good().fail()state. While skipping the last space, it hits the end, and sets botheof(because it tried to read beyond the end) andfail(because it could not read the double it wanted to).Call
.clear()afterwards to clear those error bits.The difference between the iterators comparing non-equal while
.good()is true and while.fail()is false is thatistream_iteratorwill still advance even if the stream is in aneofstate. The following further examines it:Now, after we read “2.2”, the
eofstate is set (because it tried to read beyond “2”). But the begin and end iterator won’t yet compare equal, because thefailstate is not set, and thus.fail()does not return true (while.good()would have returned false, because it considers.eof()in addition). Only after the next read, when it could not read another double anymore, thefailstate is set, and then the iterators compare equal, and the loop withinstd::copyexits.