fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. int main ()
  7. {
  8. std::vector<std::string> seq { "one", "zero", "two", "three", "zero", "four", "five" } ;
  9. const auto print = [&seq]
  10. { for( const auto& s : seq ) std::cout << s << ' ' ; std::cout << '\n' ; } ;
  11. print() ;
  12.  
  13. // replace "zero" with "nil"
  14. const std::string zero = "zero" ;
  15. const std::string nil = "nil" ;
  16. std::replace( seq.begin(), seq.end(), zero, nil ) ;
  17. print() ;
  18.  
  19. // replace strings which start with the character t with "ttttt" ;
  20. std::replace_if( seq.begin(), seq.end(),
  21. []( const std::string& s ) { return !s.empty() && s[0] == 't' ; },
  22. "ttttt" ) ;
  23. print() ;
  24. }
  25.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
one zero two three zero four five 
one nil two three nil four five 
one nil ttttt ttttt nil four five