HOWTO · C++

How to Add Int to String in C++

Learn how to append an integer to a C++ string with std::to_string, std::stringstream, and std::string::append.

On this page

To append an integer to an existing C++ string, convert the integer to text and concatenate it. For the usual case, std::to_string with std::string::operator+= is the most direct option. You can also assemble a new result with std::stringstream or convert first and call std::string::append.

Choose the right way to append an integer to a string

Use std::to_string and += when you already have a string to extend. Use std::stringstream when you are assembling text and values into a new string through stream insertion. Use std::string::append when you specifically want the string member function; it also needs string text as its argument. The examples below show each approach with the same starting text and integer.

Use std::to_string with +=

std::to_string(number) converts the numeric value into a std::string, and += appends that text to the existing string. This complete example is the recommended starting point; it prints Order number: 12345 followed by a newline.

#include <iostream>
#include <string>

int main() {
    std::string message = "Order number: ";
    int number = 12345;
    message += std::to_string(number);
    std::cout << message << '\n';
    return 0;
}
Order number: 12345

The same conversion function accepts floating-point values. Under older C++ specifications and many implementations, std::to_string(12.345) commonly produces text such as 12.345000; newer C++ versions may format floating-point values differently. Do not rely on a single representation or assume trailing fractional zeroes are removed. If you need a particular number of decimal places or trimmed formatting, use a stream with formatting controls or another deliberate formatting method.

Use std::stringstream to build a string

A std::stringstream accepts values with the insertion operator <<. Insert both the starting text and the integer, then call .str() to retrieve the assembled result. This creates a result string; it does not append to or modify the original string variable. Include <sstream> to use the stream type. This example prints Order number: 12345 followed by a newline.

#include <iostream>
#include <sstream>
#include <string>

int main() {
    std::string message = "Order number: ";
    int number = 12345;
    std::stringstream stream;
    stream << message << number;
    std::cout << stream.str() << '\n';
    return 0;
}
Order number: 12345

Use std::string::append with a converted integer

std::string::append appends character data or string text, so convert the integer before passing it. The string overload can receive the result of std::to_string directly, as shown here. This example prints Order number: 12345 followed by a newline.

#include <iostream>
#include <string>

int main() {
    std::string message = "Order number: ";
    int number = 12345;
    message.append(std::to_string(number));
    std::cout << message << '\n';
    return 0;
}
Order number: 12345

Do not confuse appending a converted integer with the append(count, character) overload: that overload adds the specified character repeatedly. For instance, message.append(3, '!') adds !!!; it does not convert the number 3 into the text 3.