Horje
How to Convert an Integer to a String in C++?

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++

// 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;
}

Output

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.




Reffered: https://www.geeksforgeeks.org


C++

Related
How to Convert std::string to Lower Case? How to Convert std::string to Lower Case?
How to Find the Difference of Two Vectors in C++? How to Find the Difference of Two Vectors in C++?
How to Access Individual Characters in a C++ String? How to Access Individual Characters in a C++ String?
How to Convert an Enum to a String in C++? How to Convert an Enum to a String in C++?
How to Check if a Vector is Empty in C++? How to Check if a Vector is Empty in C++?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14