C++: what is the optimal way to convert a double to a string?

后端 未结 12 1764
温柔的废话
温柔的废话 2020-12-16 17:09

What is the most optimal way to achieve the same as this?

void foo(double floatValue, char* stringResult)
{
    sprintf(stringResult, \"%f\", floatV         


        
相关标签:
12条回答
  • 2020-12-16 17:21

    Herb Sutter has done an extensive study on the alternatives for converting an int to a string, but I would think his arguments hold for a double as well.

    He looks at the balances between safety, efficiency, code clarity and usability in templates.

    Read it here: http://www.gotw.ca/publications/mill19.htm

    0 讨论(0)
  • 2020-12-16 17:22

    I'm sure someone will say boost::lexical_cast, so go for that if you're using boost, but it's basically the same as this anyway:

     #include <sstream>
     #include <string>
    
     std::string doubleToString(double d)
     {
        std::ostringstream ss;
        ss << d;
        return ss.str();
     }
    

    Note that you could easily make this into a template that works on anything that can be stream-inserted (not just doubles).

    0 讨论(0)
  • 2020-12-16 17:25

    This is very useful thread. I use sprintf_s for it but I started to doubt if it is really faster than other ways. I came across following document on Boost website which shows performance comparison between Printf/scanf, StringStream and Boost.

    Double to String is most common conversion we do in our code, so i'll stick with what i've been using. But, using Boost in other scenarios could be your deciding factor.

    http://www.boost.org/doc/libs/1_58_0/doc/html/boost_lexical_cast/performance.html

    0 讨论(0)
  • 2020-12-16 17:26

    On dinkumware STL, the stringstream is filled out by the C library snprintf.

    Thus using snprintf formatting directly will be comparable with the STL formatting part. But someone once told me that the whole is greater than or equal to the sum of its known parts.

    As it will be platform dependent as to whether stringstream will do an allocation (and I am quite sure that DINKUMWARE DOES NOT YET include a small buffer in stringstream for conversions of single items like yours) it is truely doubtful that ANYTHING that requires an allocation (ESPECIALLY if MULTITHREADED) can compete with snprintf.

    In fact (formatting+allocation) has a chance of being really terrible as an allocation and a release might well require 2 full read-modify-write cycles in a multithreaded environment unless the allocation implementation has a thread local small heap.

    That being said, if I was truely concerned about performance, I would take the advice from some of the other comments above, change the interface to include a size and use snprintf - i.e.

    bool 
    foo(const double d, char* const p, const size_t n){
         use snprintf......
         determine if it fit, etc etc etc.
    }
    

    If you want a std::string you are still better off using the above and instantiating the string from the resultant char* as there will be 2 allocations + 2 releases involved with the std::stringstream, std::string solution.

    BTW I cannot tell if the "string" in the question is std::string or just generic ascii chars usage of "string"

    0 讨论(0)
  • 2020-12-16 17:27

    Boost::lexical_cast<>

    0 讨论(0)
  • 2020-12-16 17:35

    _gcvt or _gcvt_s.

    0 讨论(0)
提交回复
热议问题