How to convert an int to string in C++?

C++Server Side ProgrammingProgramming

You can use the itoa function from C to convert an int to string. 

 example

#include<iostream>
int main() {
   int a = 10;
   char *intStr = itoa(a);
   string str = string(intStr);
   cout << str;
}

Output

This will give the output −

10

This will convert the integer to a string. In C++11, a new method, to_string was added that can be used for the same purpose. You can use it as follows −

Example

#include<iostream>
using namespace std;
int main() {
   int a = 10;
   string str = to_string(a);
   cout << str;
}

Output

This will give the output −

10
raja
Published on 28-Feb-2018 15:27:16
Advertisements