What will be printed out as a result of the following code execution?
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>

int main()
{
    std::string str = "To be or not to be...", str2;
    std::istringstream ist(str);
    for (int i = 0; i < 2; i++)
    {
        std::copy(std::istream_iterator<char>(ist),
                  std::istream_iterator<char>(),
                  std::back_inserter(str2) );
        ist.str(str);
    }
    std::cout << str2;
    return 0;
}
Explanation
After the first iteration through std::istringstream, the flags are set to eof and the second iteration does not change anything. No spaces are added because skipws flag is set by default. (The line ist >> std::noskipws would change this situation).

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes