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.

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

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

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.