This post will discuss how to concatenate an integer to a string object in C++.

1. Using to_string() function

The most commonly used approach to concatenate an integer to a string object in C++ is to call the std::to_string function, which can return the string representation of the specified integer.

Download  Run Code

Output:

C++17

2. Using std::stringstream function

Another good alternative is to use the std::stringstream. The following C++ program demonstrates it:

Download  Run Code

Output:

C++17

3. Using boost’s lexical_cast

We can also use a boost::lexical_cast<> to concatenate an integer to a string object, as shown below:

Download Code

Output:

C++17

That’s all about concatenating an integer to a string object in C++.