fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. int main()
  6. {
  7. std::string str = "ab*+cd*+ef*+gh*+ij*+kl*+" ;
  8. std::cout << str << '\n' ;
  9.  
  10. constexpr char asterisk = '*' ;
  11.  
  12. // erase first asterisk
  13. auto pos = str.find(asterisk) ;
  14. if( pos != std::string::npos ) str.erase(pos,1) ;
  15. std::cout << str << '\n' ;
  16.  
  17. // erase last asterisk
  18. pos = str.rfind(asterisk) ;
  19. if( pos != std::string::npos ) str.erase(pos,1) ;
  20. std::cout << str << '\n' ;
  21.  
  22. // erase all asterisk
  23. while( ( pos = str.find(asterisk) ) != std::string::npos ) str.erase(pos,1) ;
  24. std::cout << str << '\n' ;
  25.  
  26. // erase all '+'
  27. str.erase( std::remove( str.begin(), str.end(), '+' ), str.end() ) ;
  28. std::cout << str << '\n' ;
  29. }
  30.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
ab*+cd*+ef*+gh*+ij*+kl*+
ab+cd*+ef*+gh*+ij*+kl*+
ab+cd*+ef*+gh*+ij*+kl+
ab+cd+ef+gh+ij+kl+
abcdefghijkl