«Int to string» in C++: Why Does This Question Torment Even Experienced Developers?
\nImagine this: you are writing a C++ program that needs to display a number on the screen and then append text to it — for example, «Result: 42». Or you need to save a user ID (int) to a JSON file as a string. It seems simple enough, right? But here's the catch — in C++, you cannot simply write string s = 42;. The compiler will throw an error.
The question «How can I convert int to string in C++?» is one of the most popular on Stack Overflow (hundreds of thousands of views). And this is no coincidence. Unlike Python or JavaScript, where everything «magically converts itself», C++ requires explicit type conversion. But don't worry! In this article, we will cover 5 proven methods, from classics to modern C++20. You will learn which method is faster, which is more convenient, and when to use each one.
\nLet's go!
\n\nMethod #1: std::to_string — Modern and Simple (C++11 and later)
\nStarting with C++11, the standard library introduced the std::to_string function. This is the simplest and most readable way to convert int to string in C++.
#include <iostream>\n#include <string>\n\nint main() {\n int number = 2024;\n std::string str = std::to_string(number);\n std::cout << "String: " << str << std::endl; // Output: String: 2024\n return 0;\n}\nPros:
\n- \n
- Short and clear — one line of code \n
- Supports int, long, float, double, and other numeric types \n
- No need to include additional libraries (except <string>) \n
Cons:
\n- \n
- No control over formatting (number of digits, leading zeros) \n
- Slower than low-level methods if called millions of times \n
When to use: In 90% of cases. For ordinary tasks where no specific output format is needed, std::to_string is the ideal choice.
Method #2: std::stringstream — Flexible Like a Swiss Army Knife
\nIf you need not just to convert a number but also to format it (add spaces, change the number base), std::stringstream comes to the rescue. This is a class from the <sstream> library that works like a «smart string».
#include <iostream>\n#include <sstream>\n#include <string>\n\nint main() {\n int number = 255;\n std::stringstream ss;\n ss << "Value: " << number << " (hexadecimal: " << std::hex << number << ")";\n std::string result = ss.str();\n std::cout << result << std::endl;\n // Output: Value: 255 (hexadecimal: ff)\n return 0;\n}\nPros:
\n- \n
- Full control over formatting (std::hex, std::oct, std::setw, std::setprecision) \n
- Can concatenate strings and numbers on the fly \n
Cons:
\n- \n
- Slower than
std::to_stringdue to creating a stringstream object \n - Requires more code for simple conversion \n
When to use: When you need complex formatting or conversion to different number bases.
\n\nMethod #3: sprintf and snprintf — The Old-School C Method
\nAn old but reliable method inherited from the C language. The sprintf function writes a formatted string into a char array. It can also be used in C++, but you need to be careful about buffer overflow.
#include <iostream>\n#include <cstdio> // for sprintf\n#include <cstring> // for strlen\n\nint main() {\n int number = 42;\n char buffer[50];\n sprintf(buffer, "%d", number);\n std::string str(buffer);\n std::cout << "Result: " << str << std::endl;\n return 0;\n}\nImportant:
Blogs
Book Recommendations
Read also