change int to string cpp
To change an int
to a string
in C++, you can use the std::to_string()
function. Here are the steps to do so:
Declare an
int
variable and assign it a value:cpp int num = 42;
Use the
std::to_string()
function to convert theint
to astring
:cpp std::string strNum = std::to_string(num);
The
std::to_string()
function takes theint
value as an argument and returns astring
representation of that value.Now,
strNum
will contain the string representation of theint
value42
.You can use
strNum
as a regularstring
variable, performing string manipulation or printing it to the console as needed.
That's it! By following these steps, you can easily convert an int
to a string
in C++.