fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. std::istream& get_line_strip_comments( std::istream& stm, std::string& str )
  6. {
  7. if( std::getline( stm, str ) )
  8. {
  9. auto pos = str.find( "//" ) ;
  10. if( pos == 0 ) return get_line_strip_comments( stm, str ) ;
  11. else if( pos != std::string::npos ) str.erase(pos) ;
  12. }
  13. return stm ;
  14. }
  15.  
  16.  
  17. int main()
  18. {
  19. std::istringstream stm( R"(
  20. this is a line with no comments
  21. // this is a comment line
  22. some text // and then a comment
  23. more text / / this is not a comment // but this is\n)" ) ;
  24.  
  25. std::string str ;
  26. while( get_line_strip_comments( stm, str ) ) std::cout << str << '\n' ;
  27. }
  28.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
this is a line with no comments
some text 
more text / / this is not a comment