In C++, converting numbers to strings is a very common task, especially while working with user input, file handling, and competitive programming problems. Sometimes we need to convert an integer into a string for concatenation, formatting, or digit manipulation.
#include <iostream>
#include <string>
using namespace std;
int main(){
int num = 123;
string str = to_string(num);
cout << str;
return 0;
}
Output
123
Explanation: The to_string() function converts the integer 123 into the string "123".
Common Method to Converting Number to String
C++ provides multiple methods to convert a numeric value into a string. Some methods are modern and simple, while others are useful for older compilers or special formatting needs.
Method 1: Using to_string()
The to_string() function can be used to convert an integer, floating point values, or any number to a string. This function accepts a number(which can be any data type) and returns the number as the desired string.
Syntax:
string to_string(data_type value);
- Parameters: value -> Any numeric value such as int, float, double, or long.
- Return Type: Returns a string object representing the numeric value.
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
// Declaring integer
int i_val = 20;
// Declaring float
float f_val = 30.50;
// Conversion of int into string using
// to_string()
string stri = to_string(i_val);
// Conversion of float into string using
// to_string()
string strf = to_string(f_val);
// Displaying the converted strings
cout << "The integer in string is : ";
cout << stri << endl;
cout << "The float in string is : ";
cout << strf << endl;
return 0;
}
Output
The integer in string is : 20 The float in string is : 30.500000
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Method 2: Using string streams
In this method, a string stream declares a stream object which first inserts a number, as a stream into an object and then uses "str()" to follow the internal conversion of a number to a string.
Syntax:
stringstream_object << value;
string_variable = stringstream_object.str();
Parameters
- value -> Numeric value inserted into the stream.
Return Type
- The str() function returns the converted string.
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int num = 2016;
// declaring output string stream
ostringstream str1;
// Sending a number as a stream into output
// string
str1 << num;
// the str() converts number into string
string geek = str1.str();
// Displaying the string
cout << "The newly formed string from number is : ";
cout << geek << endl;
return 0;
}
Output
The newly formed string from number is : 2016
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Method 3: Using the sprintf() function
sprintf() function stores the output on the char buffer specified in the function, instead of printing the output on the console.
Syntax:
sprintf(char_array, "format_specifier", value);
Parameters
- char_array -> Character buffer where output will be stored.
- "format_specifier" -> Specifies the data type format such as %d, %f.
- value -> Numeric value to convert.
Return Type
- Returns the number of characters written into the buffer.
#include <iostream>
using namespace std;
int main()
{
// any num
int n = 12234;
// string buffer
char str[1000];
// sprintf() to print num to str buffer
sprintf(str, "%d", n);
cout << "the string is : " << str;
return 0;
}
// this code is contributed by shivanisingh
Output
the string is : 12234
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Method 4: Using boost lexical cast
Similar to string conversion, the " lexical_cast() " function remains the same, but in the 'boost lexical cast' time argument list modifies to "lexical_cast(numeric_var).
Syntax:
lexical_cast<string>(value);
Parameters
- value -> Numeric value to be converted into a string.
Return Type
- Returns the converted string value.
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declaring float
float f_val = 10.5;
// Declaring int
int i_val = 17;
// lexical_cast() converts a float into string
string strf = boost::lexical_cast<string>(f_val);
// lexical_cast() converts a int into string
string stri = boost::lexical_cast<string>(i_val);
// Displaying string converted numbers
cout << "The float value in string is : ";
cout << strf << endl;
cout << "The int value in string is : ";
cout << stri << endl;
return 0;
}
Output
The float value in string is : 10.5 The int value in string is : 17
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Comparison of Methods
| Method | Advantages | Disadvantages |
|---|---|---|
| to_string() | Simple and fast | Requires C++11 or later |
| stringstream | Flexible and safe | Slightly slower |
| sprintf() | Works in older C/C++ | Less type-safe |
| boost::lexical_cast | Easy conversion | Requires Boost library |