In C++, you can convert an int
to a string
using the std::to_string()
function from the <string>
header. Here's how you can do it:
cpp
#include <iostream>
#include <string>
int main() {
int number = 42;
std::string numberString = std::to_string(number);
std::cout << "Integer: " << number << std::endl;
std::cout << "String: " << numberString << std::endl;
return 0;
}
In this example, the std::to_string()
function is used to convert the int
variable number
to a string
called numberString
. The resulting string can then be printed or manipulated as needed. The output of the program will be:
makefile
Integer: 42
String: 42
The std::to_string()
function is convenient and commonly used when you need to convert numeric types to strings in C++.
Comments
Post a Comment