How to concatenate string and int in C++

C++ concatenate string and int

In this post, we will see how to concatenate string and int in C++

There are different methods to concatenate the values of string and integer data types. Here we have discussed some of the fast and safe methods to do so.

Using to_string() method

It is the most commonly used method for concatenating string and int in C++. We can convert the int data type to string with the help of the to_string() method and then add them.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Code:

Output:

Java2blog10

Using std::stringstream method

stringstream can be used to convert the integer data type to string. We make a function to convert the input data to a string which can be integer or string.
Code:

Output:

Java2blog10

Using lexical_cast method from the Boost libraries in C++

Boost library has a lexical_cast function to concatenate string and integer data types.
Code:

Output:

Java2blog10

Conclusion

In this article, we have discusseded different methods to concatenate the string and int in C++. to_string and lexical_cast methods are faster than the stringstream method.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.