fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. inline bool not_space(char c){
  5. return c != ' ' && ( 9 > c || c > 13 );
  6. }
  7.  
  8. inline bool my_isspace(char c){
  9. return std::isspace(static_cast<unsigned char>(c));
  10. }
  11.  
  12. template<typename FN>
  13. auto find_if(std::string & s, FN fn){
  14. return std::find_if(s.begin(), s.end(), fn);
  15. }
  16. template<typename FN>
  17. auto rfind_if(std::string & s, FN fn){
  18. return std::find_if(s.rbegin(), s.rend(), fn).base();
  19. }
  20.  
  21. inline void ltrim(std::string &s) {
  22. s.erase(s.begin(), find_if(s, not_space));
  23. }
  24.  
  25. inline void rtrim(std::string &s) {
  26. s.erase(rfind_if(s, not_space), s.end());
  27. }
  28.  
  29. void trim(std::string& s){
  30. ltrim(s);
  31. rtrim(s);
  32. }
  33.  
  34. std::string trim(std::string&& s){
  35. trim(s);
  36. return s;
  37. }
  38.  
  39. int main(){
  40. std::string s{" abc "};
  41. trim(s);
  42. std::cout << trim(std::move(s));
  43. std::cout << '"' << trim(" sdf ") << '"';
  44. }
  45.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
abc"sdf"