I am learning C++ through Accelerated C++ by Andrew Koenig and Barbara E. Moo. I am trying to understand how input operators work and their relation to STL algorithms.
Here is the piece of code that confuses me:
#include <iostream>
#include <iterator>
#include <algorithm>
#include <cctype>
bool space(char c)
{
return std::isspace(c);
}
int main()
{
std::istream_iterator<char> i = std::find_if(
std::istream_iterator<char>(std::cin),
std::istream_iterator<char>(),
space);
std::cout << *i << std::endl;
return 0;
}
The code compiles fine but when I run it on any input what it outputs is the last character entered. For example, I expected the output of 123 456
to be
but it actually is 6. Similarly, I expected the output of 123456
to be some error because we try to access an element that doesn't exist, yet the output is again . What am I missing?