fork(1) download
  1. #include <iostream>
  2. #include <sstream>
  3.  
  4. int main()
  5. {
  6. std::stringstream ifs;
  7. ifs.unsetf(std::ios::skipws);
  8. ifs.str("word by word\n this is test");
  9.  
  10. std::string word;
  11. char ws;
  12. bool isWord = false;
  13. while ((isWord = static_cast<bool>(ifs >> word)) || (ifs.clear(), ifs >> ws))
  14. {
  15. std::cout << "word: '";
  16. if (isWord)
  17. std::cout << word;
  18. else
  19. std::cout << ws;
  20. std::cout << "'\n";
  21. }
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
word: 'word'
word: ' '
word: 'by'
word: ' '
word: 'word'
word: '
'
word: ' '
word: 'this'
word: ' '
word: 'is'
word: ' '
word: 'test'