Converting an int to std::string

You can use std::to_string in C++11

int i = 3;
std::string str = std::to_string(i);

#include <sstream>
#include <string>
const int i = 3;
std::ostringstream s;
s << i;
const std::string i_as_string(s.str());

boost::lexical_cast<std::string>(yourint) from boost/lexical_cast.hpp

Work's for everything with std::ostream support, but is not as fast as, for example, itoa

It even appears to be faster than stringstream or scanf:

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

Tags:

C++

String

Int