In this article, we will learn “ how to convert Integer to String”. We can do this by using the type casting to string data type. C++ also provides built-in methods and functions that can convert integers to String.
Method 1: Using the to_string() function
This method to_string() function can be used to convert an integer to floating point values or any number to a string data type. This function accepts only one parameter, which can be any data type. This function returns the number as the desired string.
Syntax
string to_string (int val);
This function accepts one parameter, which is described below.
Parameters:
Val: Any numerical value like integer, float, double
Return Value:
A string object containing the representation of the integer value as the sequence of characters or string.
The following program demonstrates the to_string() function.
Program
#include <iostream>
#include <string>
using namespace std;
// Driver Code
int main()
{
int i_val = 50;
float f_val = 90.2;
string stri = to_string(i_val);
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 : 50
The float in string is : 90.199997
Time Complexity: O(n)
Auxiliary Space: O(n)
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Method 2: Using string stream
In this section, we will use the string stream class, which declares the stream object that inserts a number as a stream into an object. Then, we will use the str() function to convert the number to a string internally.
The following program demonstrates the stringstream example for converting the integer to a string.
Program
#include<iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int num = 2026;
stringstream str1;
str1 << num;
// the str() converts number into string
string output = str1.str();
cout << "Here is the New string: ";
cout << output << endl;
return 0;
}<strong> </strong>
Output
Here is the New string: 2026
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3: Using the sprintf() function
The sprintf() function stores the output in the char buffer instead of printing it on the console. In this section, we will print the output on the console.
The following program demonstrates the sprintf() functions.
Program
// C++ Program to illustrate the use of sprintf() for number
// to string conversion
#include <iostream>
using namespace std;
int main()
{
// any num
int n = 13535343;
// string buffer
char str[1000];
// sprintf() to print num to str buffer
sprintf(str, "%d", n);
cout << "the string is : " << str;
return 0;
}
Output
the string is: 13535343
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: Using boost lexical cast
The boost lexical function also converts Integers to strings. This function provides a convenient and type-safe way to convert between different data types, particularly between strings and other fundamental data types like integers, floats, doubles, etc.
The following program demonstrates the use of boost lexical cast:
Program
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <string>
using namespace std;
int main(){
float value1 = 30.5;
// Declaring int
int value2 = 20;
string strf = boost::lexical_cast<string>(value1);
string stri = boost::lexical_cast<string>(value2);
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 : 30.5
The int value in string is : 20
Time Complexity: O(n)
Auxiliary Space: O(n)
Conclusion
In this article, we learned various approaches to converting Integers to Strings in the C++ programming language. Each method has its own merits, and the choice depends on factors such as code readability, performance requirements, and project dependencies. The “to_string()” method offers a good balance of simplicity and efficiency.
FAQs
How do I convert an integer to a string in C++?
We can use the to_string() method function, which is provided by the C++ language Standard library. It takes input as an integer and returns its string representations.
Are there alternative methods for converting integers to strings?
Yes, we can also use the stringstream method to convert an integer to a string for more complex formatting needs or boost lexical cast for type-safe conversions.
Do I need to include any specific headers for these conversion methods?
Yes, for “to_string()” include the “” header. For “stringstream”, include ‘’ and for Boost.Lexical_cast, include the appropriate boost header (‘’).
Are there any considerations when converting negative integers to strings?
Negative integers will be converted to their strings representation with a minus sign (‘-’ at the beginning. Ensure that your code accounts for negative numbers if needed.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.