C ++ How to change an integer to a string?

Possible duplicate:
An alternative to itoa () to convert an integer to a C ++ string?

How do you change an integer per line in C ++?

+3
source share
2 answers

Standard C ++ library style:

#include <sstream>
#include <string>

(...)

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

Or, if you are fortunate enough to use C ++ 11:

#include <string>

(...)

int number = 5;
std::string numberAsString = std::to_string(number);
+4
source

You can use snprintf(char *str, size_t size, const char *format, ...)to get char [] and then use string(char*)get string. Of course, there are other ways.

0
source

All Articles