title: efficiency of using stringstream to convert string to int?
body: <p>Is the code below less (or more, or equally) efficient than:</p>
  
  <pre><code>make substring from cursor
  make stringstream from substring
  extract integer using stream operator
  </code></pre>
  
  <p>? (question edit) or is it less (or more, or equally) efficient than:</p>
  
  <pre><code>std::stoi
  </code></pre>
  
  <p>? and why?</p>
  
  <p>Could this function be made more efficient?</p>
  
  <p>(The class brings these into scope:)</p>
  
  <pre><code>std::string expression  // has some numbers and other stuff in it
  int cursor              // points somewhere in the string
  </code></pre>
  
  <p>The code:</p>
  
  <pre><code>int Foo_Class::read_int()
  {
      /** reads an integer out of the expression from the cursor */
  
      // make stack of digits
      std::stack&lt;char&gt; digits;
  
      while (isdigit(expression[cursor]))  // this is safe, returns false, for the end of the string (ISO/IEC 14882:2011 21.4.5)
      {
          digits.push(expression[cursor] - 48);  // convert from ascii
          ++cursor;
      }
  
      // add up the stack of digits
      int total = 0;
      int exponent = 0;  // 10 ^ exponent
      int this_digit;
  
      while (! digits.empty())
      {
          this_digit = digits.top();
          for (int i = exponent; i &gt; 0; --i)
              this_digit *= 10;
          total += this_digit;
  
          ++exponent;
          digits.pop();
      }
  
      return total;
  }
  </code></pre>
  
  <p>(I know it doesn't handle overflow.)</p>
  
  <p>(I know someone will probably say something about the magic numbers.)</p>
  
  <p>(I tried pow(10, exponent) and got incorrect results. I'm guessing because of floating point arithmetic, but not sure why because all the numbers are integers.)</p>