[ACCEPTED]-c++ integer->std::string conversion. Simple function?-stdstring

Accepted answer
Score: 135

Now in c++11 we have

#include <string>
string s = std::to_string(123);

Link to reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string


Score: 28

Like mentioned earlier, I'd recommend boost 2 lexical_cast. Not only does it have a fairly 1 nice syntax:

#include <boost/lexical_cast.hpp>
std::string s = boost::lexical_cast<std::string>(i);

it also provides some safety:

  std::string s = boost::lexical_cast<std::string>(i);
}catch(boost::bad_lexical_cast &){
Score: 22

Not really, in the standard. Some implementations 5 have a nonstandard itoa() function, and 4 you could look up Boost's lexical_cast, but 3 if you stick to the standard it's pretty 2 much a choice between stringstream and sprintf() (snprintf() if 1 you've got it).

More Related questions