3

Possible Duplicate:
converting a string into a double

What is the optimal way to convert string to double ? am using sstream but I feel it is not that fast.. is there a faster wawy without using any external libraries or sth.

Community
  • 1
  • 1
Loers Antario
  • 1,611
  • 6
  • 20
  • 24

2 Answers2

3

strtod() will have less overhead than using sstream.

#include <stdlib.h>

char * s = ...;
double d = strtod(s, 0);
Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
  • 2
    Of course, if his input is in a `string`, he'll have to use `c_str()` to get it. And it needs some error checking. – James Kanze Jul 13 '12 at 13:47
0

you can use boost library lexical cast They also include performance test results on their documentation page boost lexical cast

cheers

Moataz Elmasry
  • 2,509
  • 4
  • 27
  • 37
  • @Mike: Early versions did, newer versions have overrides for a number of types, including double. It only falls back to using the stringstream if it doesn't have its own mechanism for performing the conversion. – Dave S Jul 13 '12 at 14:36
  • @Mike, thats right, in their test results they are comparing their method to sstream and showing that lexicalcast is f#aster – Moataz Elmasry Jul 13 '12 at 14:42