c++ int to string
To convert an integer to a string in C++, you can use the std::to_string()
function. This function takes an integer as its argument and returns a string representation of that integer.
Here are the steps to convert an integer to a string in C++ using std::to_string()
:
Include the necessary header file:
#include <string>
. This header file provides the definition of thestd::to_string()
function.Declare an integer variable and assign a value to it, e.g.,
int num = 12345;
. This is the integer that you want to convert to a string.Call the
std::to_string()
function, passing the integer variable as its argument, e.g.,std::string str = std::to_string(num);
. The function returns a string representation of the integer.You now have the integer converted to a string. You can use the
str
variable to manipulate or display the converted string as desired.
Note: Make sure to include the <string>
header file and use the std::
namespace prefix when calling the std::to_string()
function.
That's it! By following these steps, you can convert an integer to a string in C++ using the std::to_string()
function.