Guest User

dave88

a guest
Jun 7th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. void TrimStringLeft(std::string& str)
  4. {
  5.    int i = 0;
  6.    int e = (int)str.size();
  7.    for (i; i!=e; ++i)
  8.    {
  9.       if (!isspace(str[i]))
  10.          break;
  11.    }
  12.    if (i!=0 && i<e)
  13.       str = str.substr(i);
  14. }
  15.  
  16. std::string TrimStringLeft2(const std::string& str)
  17. {
  18.     size_t cut(0);
  19.     for (std::string::const_iterator it = str.begin(); it != str.end() && isspace(*it); ++cut, ++it);
  20.     return str.substr(cut);
  21. }
  22.  
  23. int main(int argc, char *argv[])
  24. {
  25.     const std::string s(argc > 1 ? argv[1] : "   abc");
  26.     std::string s1(s), s2(s);
  27.     TrimStringLeft(s1);
  28.     s2 = TrimStringLeft2(s2);
  29.     std::cout << s1 << std::endl;
  30.     std::cout << s2 << std::endl;
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment