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)

POSTGRADUATE PROGRAM IN
Multi Cloud Architecture & DevOps
Master cloud architecture, DevOps practices, and automation to build scalable, resilient systems.
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)

82.9%
of professionals don't believe their degree can help them get ahead at work.
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.
How do I convert an integer to a string in C++?
Are there alternative methods for converting integers to strings?
Do I need to include any specific headers for these conversion methods?
Are there any considerations when converting negative integers to strings?
Updated on July 16, 2024
