The following program:Becase std::istream_iterator<chardoes not mean "return one character at a
#include <iostream>
#include <iterator>
#include <istream>
#include <sstream>
int main()
{
char buffer[] = {0x01, 0x0C, 0x1B};
std::stringbuf str_buf;
str_buf.pubsetbuf(buffer, sizeof (buffer));
std::istream iss(&str_buf);
std::cout.setf( std::ios::hex, std::ios::basefield );
std::copy(std::istream_iterator<char>(iss),
std::istream_iterator<char>(), std::ostream_iterator<unsigned
short>(std::cout, " "));
return (0);
Generates the output (gcc 4.2 on Linux):
1 1b
Why it does not print the 0x0C value?
time from a std::istream". It means "return the next element from a
std::istream, where each element is a single character, and all elements
are separated by whitespace characters".
It just so happens that 0x0c is the ascii formfeed character, which is a
whitespace character, to it is interpreted as a delimitor between the 0x01
and the 0x1b character.
Note that your std::ostream_iterator<unsigned shortproduces, on output,
whitespace-delimited integers. If you take this output, and supply it, as a
stream, to a suitably-configured std::istream_iterator<unsigned short>
(ignoring, for the moment the issue of hexadecimal vs decimal>, you will end
up iterating back over the same integers, with the iterator automatically
skipping the space characters that delimit your integers. Replace "integers"
with "chars", and that's what's happening with your input iterator.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEABECAAYFAkimChMACgkQx9p3GYHlUOL30gCfSitAztWAZD zMkdedaaxNAOcc
mrEAnRihiP/0lgEHsltyyQs3tHgBmSPs
=du9A
-----END PGP SIGNATURE-----