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
19 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
92 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
• 16,850 points
30 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

5 days ago in Others by Kichu
• 16,850 points
4 views
0 votes
1 answer
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
508 views
0 votes
0 answers

How can I prevent java.lang.NumberFormatException: For input string: "N/A"?

 I am getting a NumberFormatException: java.lang.NumberFormatException: For input string: ...READ MORE

Apr 30 in Other DevOps Questions by Kichu
• 16,850 points
46 views
0 votes
1 answer

Find length of string in R

You can try nchar > nchar("fast") [1] 4 > set.seed(10) > ...READ MORE

answered Apr 17, 2018 in Data Analytics by DataKing99
• 8,240 points
244 views
0 votes
1 answer

Removing all white-spaces from a string

You can use the 'str_replace_all()' function from ...READ MORE

answered May 16, 2018 in Data Analytics by Bharani
• 4,660 points
188 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
46 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
26 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