In C++, converting integers to strings or converting numbers to strings or vice-versa is actually a big paradigm shift in itself. In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number. Let’s look at some methods to convert an integer or a number to a string.
Converting Number to String in C++
There are 4 major methods to convert a number to a string, which are as follows:
- Using to_string()
- Using string Stream
- Using sprintf() function
- Using boost lexical cast
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 (int val);
Parameters:
- val – Any Numerical Value.
Return Value:
- A string object containing the representation of value as a sequence of characters.
Example:
C++
#include <iostream>
#include <string> // for string and to_string()
using namespace std;
int main()
{
int i_val = 20;
float f_val = 30.50;
string stri = to_string(i_val);
string strf = to_string(f_val);
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.
Example:
C++
#include<iostream>
#include <sstream> // for string streams
#include <string> // for string
using namespace std;
int main()
{
int num = 2016;
ostringstream str1;
str1 << num;
string geek = str1.str();
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.
C++
#include <iostream>
using namespace std;
int main()
{
int n = 12234;
char str[1000];
sprintf (str, "%d" , n);
cout << "the string is : " << str;
return 0;
}
|
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).
Example:
C++
#include <boost/lexical_cast.hpp> // for lexical_cast()
#include <iostream>
#include <string> // for string
using namespace std;
int main()
{
float f_val = 10.5;
int i_val = 17;
string strf = boost::lexical_cast<string>(f_val);
string stri = boost::lexical_cast<string>(i_val);
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)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
18 May, 2023
Like Article
Save Article