Easiest way to convert int to string in C

0 votes

What is the easiest way to convert from int to equivalent string in C++. I am aware of two methods. Is there any easier way?

(1)
int a = 10; 
char *intStr = itoa(a); 
string str = string(intStr);
(2)
int a = 10; 
stringstream ss; ss << a; 
string str = ss.str();
Feb 10 in Others by Soham
• 8,730 points
32 views

1 answer to this question.

0 votes

The C++11 version has introduced the std::stoi along with variants for each numeric type and the std::to_string which are the counterparts of the C atoi and itoa but are expressed in terms of std::string.

#include <string> std::string s = 

std::to_string(42);

So to answer your question, this is the shortest way I can think of. You could even try to omit naming the type, by using the auto keyword:

 

auto s = std::to_string(42);

answered Feb 10 by Rahul
• 8,980 points

Related Questions In Others

0 votes
1 answer

Convert char to int in C and C++

In order to read the value as ...READ MORE

answered Feb 17 in Others by Aditya
• 7,280 points
340 views
0 votes
0 answers

How to convert a string to an integer in JavaScript?

How do I convert a string to ...READ MORE

May 2 in Others by Kichu
• 19,040 points
47 views
0 votes
0 answers

What is the best way to use a HashMap in C++?

Can someone recommend me some good documentation ...READ MORE

May 19 in Others by Kichu
• 19,040 points
16 views
0 votes
1 answer
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 4,240 points
28 views
0 votes
1 answer

Easiest way to convert int to string in C++

std::stoi (and variations for each numeric type) ...READ MORE

answered Jun 15 in C++ by Damon
• 4,240 points
15 views
0 votes
1 answer

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

answered Jun 20 in C++ by Damon
• 4,240 points
7 views
0 votes
1 answer

How to convert a Unicode string to string

It can be done in the following ...READ MORE

answered Oct 16, 2018 in Python by SDeb
• 13,300 points
527 views
0 votes
1 answer

How to install OpenSSL in windows 10?

I can answer your doubt as I ...READ MORE

answered Feb 10 in Others by Rahul
• 8,980 points
125 views
0 votes
1 answer

How to create a Custom Dialog box in android?

Here I have created a simple Dialog, ...READ MORE

answered Feb 18 in Others by Rahul
• 8,980 points
41 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP