Avatar ·

How to convert int to string in C++? (converting integer to string)

Hello, colleagues! I'm a beginner in C++ and encountered a seemingly simple task: I need to convert an integer (int) to a string (std::string). In other languages, like Python or Java, this is done with a single built-in function. But in C++ I'm confused. The problem occurs in the following code: ```cpp #include #include int main() { int number = 42; std::string text = "Answer: " + number; // Error! std::cout << text << std::endl; return 0; } ``` The compiler gives an error: "invalid operands to binary expression ('std::string' and 'int')". I understand that C++ is strongly typed, and you can't just concatenate a string with a number. But how do you do it correctly? I've heard about some functions like `itoa()`, `sprintf()`, `stringstream` and `std: