Easiest way to convert int to string in C++

后端 未结 28 1747
甜味超标
甜味超标 2020-11-21 06:42

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

(1)



        
28条回答
  •  情歌与酒
    2020-11-21 07:29

    Probably the most common easy way wraps essentially your second choice into a template named lexical_cast, such as the one in Boost, so your code looks like this:

    int a = 10;
    string s = lexical_cast(a);
    

    One nicety of this is that it supports other casts as well (e.g., in the opposite direction works just as well).

    Also note that although Boost lexical_cast started out as just writing to a stringstream, then extracting back out of the stream, it now has a couple of additions. First of all, specializations for quite a few types have been added, so for many common types, it's substantially faster than using a stringstream. Second, it now checks the result, so (for example) if you convert from a string to an int, it can throw an exception if the string contains something that couldn't be converted to an int (e.g., 1234 would succeed, but 123abc would throw).

    As of C++11, there's a std::to_string function overloaded for integer types, so you can use code like:

    int a = 20;
    std::string s = std::to_string(a);
    // or: auto s = std::to_string(a);
    

    The standard defines these as being equivalent to doing the conversion with sprintf (using the conversion specifier that matches the supplied type of object, such as %d for int), into a buffer of sufficient size, then creating an std::string of the contents of that buffer.

提交回复
热议问题