Skip to main content
Corrected - no point extracting end iterator
Source Link
William Morris
  • 9.4k
  • 19
  • 43

I'm not a C++ guru, but for what they are worth, I have some comments:

  • it would be better to evaluate s.end() just once (outside the loop). Correction: this won't help.

  • the function fails if s starts with a space

  • you have duplicate code turning an int into a string. Perhaps use C++11 to_string or write your own a function, such as:

      template <typename T>
      static std::string to_string(T num)
      {
          std::stringstream ss;
          ss << num;
          return ss.str();
      }
    

I'm not a C++ guru, but for what they are worth, I have some comments:

  • it would be better to evaluate s.end() just once (outside the loop)

  • the function fails if s starts with a space

  • you have duplicate code turning an int into a string. Perhaps use C++11 to_string or write your own a function, such as:

      template <typename T>
      static std::string to_string(T num)
      {
          stringstream ss;
          ss << num;
          return ss.str();
      }
    

I'm not a C++ guru, but for what they are worth, I have some comments:

  • it would be better to evaluate s.end() just once (outside the loop). Correction: this won't help.

  • the function fails if s starts with a space

  • you have duplicate code turning an int into a string. Perhaps use C++11 to_string or write your own a function, such as:

      template <typename T>
      static std::string to_string(T num)
      {
          std::stringstream ss;
          ss << num;
          return ss.str();
      }
    
Source Link
William Morris
  • 9.4k
  • 19
  • 43

I'm not a C++ guru, but for what they are worth, I have some comments:

  • it would be better to evaluate s.end() just once (outside the loop)

  • the function fails if s starts with a space

  • you have duplicate code turning an int into a string. Perhaps use C++11 to_string or write your own a function, such as:

      template <typename T>
      static std::string to_string(T num)
      {
          stringstream ss;
          ss << num;
          return ss.str();
      }