Convert Integer to String in C++
Convert Integer to String in C++ (5 Programs).
1#include <iostream>2#include <string>3#include <sstream>4using namespace std;56int main() {7 int num = 12345;89 // Method 1: Using to_string()10 string str1 = to_string(num);1112 // Method 2: Using stringstream13 stringstream ss;14 ss << num;15 string str2 = ss.str();1617 cout << "Integer: " << num << endl;18 cout << "String (method 1): " << str1 << endl;19 cout << "String (method 2): " << str2 << endl;2021 return 0;22}
Output
Integer: 12345 String (method 1): 12345 String (method 2): 12345
What's going on
The program converts an integer (like 12345) into a string (like "12345"). This conversion is necessary because:
Example:
Header Files Used
Declaring Variables
The program declares:
int num = 12345;
Method 1: Using to_string() (Integer to String)
string str1 = to_string(num);
This is the most modern and recommended method in C++:
How it works:
Advantages:
Example:
int n = 12345;
string s = to_string(n); // s = "12345"
Method 2: Using stringstream
stringstream ss;
ss << num;
string str2 = ss.str();
This method uses stringstream to convert the integer:
How it works:
Advantages:
Example with formatting:
stringstream ss;
ss << fixed << setprecision(2) << 123.45;
string s = ss.str(); // s = "123.45"
Example with multiple values:
stringstream ss;
ss << "Number: " << 12345 << " is an integer";
string s = ss.str(); // s = "Number: 12345 is an integer"
Other Methods (Mentioned but not shown in code)
Method 3: Using sprintf() (C-style)
char buffer[20];
sprintf(buffer, "%d", num);
string str3 = buffer;
1#include <cstdio>
Method 4: Using itoa() (Non-standard, C-style)
char buffer[20];
itoa(num, buffer, 10); // 10 = base 10 (decimal)
string str4 = buffer;
1#include <cstdlib>
Method 5: Manual Conversion
string str5 = "";
int temp = num;
if (temp == 0) str5 = "0";
else {
bool negative = temp < 0;
if (negative) temp = -temp;
while (temp > 0) {
str5 = char('0' + temp % 10) + str5;
temp /= 10;
}
if (negative) str5 = "-" + str5;
}
How Manual Conversion Works (Step-by-Step)
Understanding manual conversion helps you understand the concept:
For integer 12345:
The process:
Displaying Results
The program prints:
Output:
Integer: 12345
String (method 1): 12345
String (method 2): 12345
Both methods produce the same string value.
1cout << "Integer: " << num << endl;2cout << "String (method 1): " << str1 << endl;3cout << "String (method 2): " << str2 << endl;
When to Use Each Method
-
to_string()
: Best for most cases - simple, safe, modern C++
-
stringstream
: Best when you need formatting or combining multiple values
-
sprintf()
: Only for legacy code or when you need C compatibility
-
itoa()
: Avoid - not standard, not portable
-
Manual
: For learning or when you need custom conversion logic
Formatting Considerations
With to_string()
: Basic conversion, no formatting options
string s = to_string(123.456); // "123.456000" (default precision)
With stringstream
: Full formatting control
stringstream ss;
ss << fixed << setprecision(2) << 123.456;
string s = ss.str(); // "123.46"
For integers
: to_string() is usually sufficient
For floats/doubles
: stringstream gives better control over decimal places
Performance Considerations
-
to_string()
: Fastest and most efficient for simple conversions
-
stringstream
: Slightly slower but more flexible
-
sprintf()
: Fast but less safe (buffer management)
-
Manual
: Slowest but most educational
For most applications, the performance difference is negligible. Choose based on code clarity and requirements.
return 0;
This ends the program successfully.