fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. int main()
  6. {
  7. //std::istringstream iss(" strings with leading and trailing spaces ");
  8. std::istringstream iss(" John Doe \n Mary Smith");
  9.  
  10. std::string lines[2];
  11. size_t i = 0;
  12. bool keep_trailing_spaces = false;
  13. while (std::getline(iss, lines[i++], '\n'))
  14. {
  15. if (i > 1)
  16. {
  17. keep_trailing_spaces = true;
  18. break;
  19. }
  20. }
  21.  
  22. if (i > 1)
  23. {
  24. size_t start = lines[0].find_first_not_of(' ');
  25. size_t count = keep_trailing_spaces ? std::string::npos : lines[0].find_last_not_of(' ') - start + 1;
  26.  
  27. std::cout << ">" << lines[0].substr(start, count) << "<" << std::endl;
  28. }
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
>John Doe    <