How to Concatenate std::string and int in C++: Non-Obvious Pitfalls
\n\nHave you ever written something like "Hello" + 42 in C++ and gotten a strange set of characters instead of the expected "Hello42"? If so, you are not alone. This question on Stack Overflow has gathered tens of thousands of views, and for good reason. In C++, concatenating a string and a number is not as trivial a task as in Python or JavaScript. Here you need to know a few tricks.
In this article, we will break down 4 ways to concatenate std::string and int, from the simplest to the professional. You will learn why str + num doesn't work, how to work around this limitation, and which method to choose for your task. Let's go!
Why doesn't str + int work? A quick look at types
\n\nIn C++, std::string is a class, and int is a built-in numeric type. The + operator for std::string is overloaded, but it expects the second operand to also be a string type (std::string, const char*, or a character). When you write "Hello" + 42, the compiler tries to interpret 42 as an arithmetic offset for a pointer to a string literal, not as a number for insertion.
The result? Undefined behavior, program crash, or garbage in memory. This is precisely why we need special conversion methods.
\n\nMethod 1: std::to_string — The Simplest (C++11 and newer)
\n\nStarting from C++11, the standard library introduced the wonderful function std::to_string. It converts any number (int, float, double, long, etc.) into a std::string. After that, it's a simple concatenation of two strings.
#include <iostream>\n#include <string>\n\nint main() {\n std::string text = "The number Pi is approximately ";\n int pi_int = 3;\n\n // Convert int to string and concatenate\n std::string result = text + std::to_string(pi_int);\n std::cout << result << std::endl; // Output: The number Pi is approximately 3\n\n // Can be done in one line\n std::cout << "Answer: " + std::to_string(42) << std::endl;\n\n return 0;\n}\n\nPros: readable, concise, no need to include additional libraries (besides <string>).
\nCons: no formatting control (number of decimal places, leading zeros).
Method 2: std::ostringstream — The King of Formatting
\n\nIf you need not just to concatenate but also to format the number (add leading zeros, limit the fractional part), use std::ostringstream from the <sstream> header. This approach mimics the operation of cout but saves the result into a string.
#include <iostream>\n#include <sstream>\n#include <string>\n#include <iomanip> // for std::setw and std::setfill\n\nint main() {\n int user_id = 7;\n std::ostringstream oss;\n\n // Format: ID with leading zeros up to 3 digits\n oss << "User #" << std::setw(3) << std::setfill('0') << user_id;\n std::string formatted = oss.str();\n\n std::cout << formatted << std::endl; // Output: User #007\n\n // More complex example: fractional number\n double price = 19.5;\n std::ostringstream oss2;\n oss2 << "Price: " << std::fixed << std::setprecision(2) << price << " rub.";\n std::cout << oss2.str() << std::endl; // Output: Price: 19.50 rub.\n\n return 0;\n}\n\nPros: full formatting control, works with any types.
\nCons: more verbose code, need to include <sstream> and <iomanip>.
Method 3: sprintf / snprintf — Legacy from C (Caution!)
\n\nIf you are working with legacy code or for some reason cannot use