How to do integer -> string conversion has been answered many times on the internet… however, I’m looking for the most compact “C++-way” to do this.
Since you’re able to concatenate strings using the overloaded + operator, it would be preferable to be able to do something along the lines of the python-ish
x = (stringVariable + str(intVariable)) concatenation, but I don’t know if there’s a canonical way to do this in C++.
The most common solutions I see are:
stringstream: If possible, it would be nice not to have 3 lines of code (declaration, writing to the stream, conversion to string) just to concatenate some letters and numbers.
itoa: this works, but I’m looking for a canonical C++ solution. Also, I think itoa is technically non-standard although I could be wrong.
boost format / boost lexical cast: this works too, but is there nothing in vanilla C++ that does the job?
String to integer:
int n = std::stoi(s);Integer to string:
std::string s = std::to_string(n);