Easiest way to convert int to string in C++

+1077 votes
asked Apr 8, 2011 by nemo

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?


int a = 10;
char *intStr = itoa(a);
string str = string(intStr);


int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

20 Answers

+17 votes
answered Apr 8, 2011 by mehrdad

Not that I know of, in pure C++. But a little modification of what you mentioned

string s = string(itoa(a));

should work, and it's pretty short.

+78 votes
answered Apr 8, 2011 by jerry-coffin

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<string>(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 = 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.

+15 votes
answered Apr 8, 2011 by throwback1986

sprintf() is pretty good for format conversion. You can then assign the resulting C string to the C++ string as you did in 1.

+147 votes
answered Apr 8, 2011 by devsolar

Since "converting ... to string" is a recurring problem, I always define the SSTR() macro in a central header of my C++ sources:

#include <sstream>

#define SSTR( x ) static_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()

Usage is as easy as could be:

int i = 42;
std::cout << SSTR( "i is: " << i );
std::string s = SSTR( i );
puts( SSTR( i ).c_str() );

The above is C++98 compatible (if you cannot use C++11 std::to_string), and does not need any third-party includes (if you cannot use Boost lexical_cast<>); both these other solutions have a better performance though.

+38 votes
answered Apr 8, 2011 by kevin

If you have Boost installed (which you should):

#include <boost/lexical_cast.hpp>

int num = 4;
std::string str = boost::lexical_cast<std::string>(num);
+1453 votes
answered Apr 8, 2011 by matthieu-m

C++11 introduces std::stoi (and variants for each numeric type) and std::to_string, the counterparts of the C atoi and itoa but expressed in term of std::string.

#include <string> 

std::string s = std::to_string(42);

is therefore the shortest way I can think of. You can even omit naming the type, using the auto keyword:

auto s = std::to_string(42);

Note: see [string.conversions] (21.5 in n3242)

+90 votes
answered Nov 29, 2012 by rasoul

I usually use the following method:

#include <sstream>

template <typename T>
  std::string NumberToString ( T Number )
     std::ostringstream ss;
     ss << Number;
     return ss.str();

described in details here.

–1 vote
answered Feb 6, 2013 by user2047065
char * bufSecs = new char[32];
char * bufMs = new char[32];
+2 votes
answered Feb 21, 2013 by gilad-darmon
namespace std
    inline string to_string(int _Val)
    {   // convert long long to string
        char _Buf[2 * _MAX_INT_DIG];
        snprintf(_Buf, "%d", _Val);
        return (string(_Buf));

you can now use to_string(5)

+25 votes
answered Apr 16, 2013 by user2287915

Wouldn't it be easier using stringstreams?

#include <sstream>

int x=42;            //The integer
string str;          //The string
ostringstream temp;  //temp as in temporary
str=temp.str();      //str is temp as string

Or make a function:

#include <sstream>

string IntToString (int a)
    ostringstream temp;
    return temp.str();
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter