I have the following code:
#include <sstream>
#include <iterator>
#include <iostream>
int main()
{
std::stringstream str; str << "abc\ndef";
std::cout << "[" << str.str() << "]" << std::endl;
std::istream_iterator<char> it(str), end;
for (; it != end; ++it)
{
std::cout << "[" << unsigned(*it) << "]";
}
std::cout << std::endl;
return 0;
}
And the output is:
[abc
def]
[97][98][99][100][101][102]
Why std::istream_iterator ignored the new-line character?
Because istream_iterator
uses operator>>
. And istream::operator>>(char)
skips whitespace, unless you unset the skipws
flag of the stream. (e.g. using noskipws
)
It's the same output you would get if you did this:
char c;
while (str >> c)
std::cout << "[" << unsigned(c) << "]";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With