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, 2022 in Others by Soham
• 9,710 points
539 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, 2022 by Rahul
• 9,680 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, 2022 in Others by Aditya
• 7,680 points
3,716 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, 2022 in Others by Kichu
• 19,040 points
533 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, 2022 in Others by Kichu
• 19,040 points
477 views
0 votes
1 answer

What is the easiest way to convert an Excel spreadsheet with tabular data to JSON?

Assuming you really mean easiest and are not necessarily ...READ MORE

answered Dec 29, 2022 in Others by narikkadan
• 63,600 points
13,497 views
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, 2022 in C++ by Damon
• 4,960 points
1,422 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, 2022 in C++ by Damon
• 4,960 points
1,107 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, 2022 in C++ by Damon
• 4,960 points
5,657 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
1,262 views
0 votes
1 answer

How to install OpenSSL in windows 10?

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

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
2,324 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, 2022 in Others by Rahul
• 9,680 points
1,126 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP