5

Possible Duplicate:
Alternative to itoa() for converting integer to string C++?

How do you change an integer to a string in c++?

Community
  • 1
  • 1
Boom_mooB
  • 97
  • 1
  • 1
  • 6

2 Answers2

6

Standard C++ library style:

#include <sstream>
#include <string>

(...)

int number = 5;
std::stringstream ss;
ss << number;
std::string numberAsString(ss.str());

Or if you're lucky enough to be using C++11:

#include <string>

(...)

int number = 5;
std::string numberAsString = std::to_string(number);
Asik
  • 21,506
  • 6
  • 72
  • 131
0

You could use snprintf(char *str, size_t size, const char *format, ...) to get a char[], then use string(char*) get string. Of course,there're other ways.

twain249
  • 5,666
  • 1
  • 21
  • 26
DavidChuBuaa
  • 41
  • 1
  • 5