Integer and String are the two data types in C++ programming languages. An integer is generally used to represent the whole numbers in the number line. A String is a data structure that is used to save a collection of characters. It can be a word or a sentence. Moreover, it can also store the numerical characters representing an integer number. In this article, we are going to learn how to convert an integer to a string in C++.
Example:
Input: 42(Integer)
Output: 42(String)
How to Convert an Integer to a String in C++?
To convert an int data type to a string data type, we can use the to_string() function. This function is a part of <string> header. This function is used to convert any data structure to a string.
C++ Program to Convert an Integer to a String
// C++ Program to Convert an Integer to a String #include <iostream> #include <string> using namespace std;
// Driver Code int main()
{ int num = 42;
// converting int to string
string str = to_string(num);
cout << "Integer to String using to_string(): " << str
<< endl;
return 0;
} |
Integer to String using to_string(): 42
Time Complexity: O(logN)
Space Complexity: O(k), where k is the number of digits in the integer N.