Integer type variables in C can store positive or negative integer values within a predefined range. String variables can store sequences of letters, numbers, and special characters. There are many use cases where conversion from int to string is required. We discuss 3 different ways to convert an integer variable to a string.
If we talk about the algorithm, it is very simple. We get the input in an integer variable and convert it to a string variable.
The easiest way to convert an integer value to a string in C is to use the to_string function. The to_string function is available by default; it accepts an integer value as input and provides a string value as output. Let’s look at the following example to understand this concept further.
int ip = <integer value>; string op = std::to_string(ip);
#include <iostream> using namespace std; string solve(int ip) { //using the to_string function return to_string(ip); } int main() { int ip = 10; string op = solve(ip); cout<< "The input value is: " << ip << endl; cout<< "The output value is: " + op << endl; return 0; }
The input value is: 10 The output value is: 10
In this example, we use the to_string function to convert an integer value to a string. One thing to note when displaying the output; we use the insertion operator (<<) to display integer values, but when we display a string, we just concatenate the output value with the previous string to prove that it is a string. <<) 来显示整数值,但是当我们显示字符串时,我们只是将输出值与前面的字符串连接起来以证明它是一个字符串。
ostringstream is a string buffer containing a sequence of characters. In this method, we input the integer value into the ostringstream object and then format it into a string.
int ip = <integer value>; ostringstream oss; oss << ip; string op = oss.str();
#include <iostream> #include <sstream> using namespace std; string solve(int ip) { //using ostringstream ostringstream oss; oss << ip; return oss.str(); } int main() { int ip = 10; string op = solve(ip); cout<< "The input value after addition of 10 is: " << ip + 10 << endl; cout<< "The output value after addition of 10 is: " << op + "10" << endl; return 0; }
The input value after addition of 10 is: 20 The output value after addition of 10 is: 1010
In the previous example, we added an integer value of 10 to the input value to indicate that it is an integer value, and added a string "10" to the output value to indicate that it is a string value.
sprintf is a standard library function in C that sends formatted output to the string str. Using sprintf function we can convert integer to string.
int ip = <integer value>; char str[100]; sprintf(str, "%d", ip); string s = str;
#include <iostream> using namespace std; string solve(int ip) { char str[100]; sprintf(str, "%d", ip); string s = str; return s; } int main() { int ip = 10; string op = solve(ip); cout<< "The input value after addition of 10 is: " << ip + 10 << endl; cout<< "The output value after addition of 10 is: " << op + "10" << endl; return 0; }
The input value after addition of 10 is: 20 The output value after addition of 10 is: 1010
This example is similar to the previous example, the only difference is the conversion method. To use sprintf, we don't need to import any other libraries.
We may need to convert integers to strings on various occasions, mainly to output data from calculations using functions that only support string parameters. The first method we discussed is the simplest, but it is available starting from C 11 version. The second method using ostringstream requires importing another library sstream, while the last method using sprintf requires a auxiliary character or string buffer to convert the integer value to a string. The fastest way is the first, but if that doesn't work due to an outdated compiler, the other two should work.
The above is the detailed content of C++ program to convert int type variable to string. For more information, please follow other related articles on the PHP Chinese website!