+ 2

How to convert

Just to share String conversion to an integer or vice versa is a common phenomenon of everyday programming. C++ 11 introduced some convenient wrappers for the purpose. For example, to convert an integer to a string, we may write the following: #include<string> int main() { int ival=112233; string str=to_string(ival); return 0; }

1st Nov 2017, 6:48 AM
dagem
dagem - avatar
8 ответов
+ 19
(Oh damn......)
2nd Nov 2017, 5:24 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 16
int val = 123; string vl = val+""; //wrong
1st Nov 2017, 7:46 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 14
@KV the complete code kinshuk posted gives a warning there is no output on clang++ test.cpp:22:21: warning: adding 'int' to a string does not append to the string [-Wstring-plus-int] string res = 123+""; test.cpp:22:21: note: use array indexing to silence this warning 1 warning generated.
2nd Nov 2017, 1:23 AM
Lord Krishna
Lord Krishna - avatar
+ 8
@~swim~ I really am getting nothing. I don't know why. Did you run a similar code as well? See this : https://drive.google.com/folderview?id=0B1zDeW2sXou9bzVEejhvZ0pOZ2c But All I can guess is that you cannot convert integer to string from this method. It is incrementing the pointer of the const string instead.
2nd Nov 2017, 12:47 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 8
eh? I think It's no good for int+str Polyfill for sl https://code.sololearn.com/c8gkAjpCDscI/?ref=app
2nd Nov 2017, 1:31 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar
+ 7
@~swim~ Yeah, that code must be for JavaScript. But the weirdest part is that 'dagem' never verified the same, nor did anyone else. Perhaps I was wrong to do so. @ValentinHacker Sorry for any inconvenience caused. I just couldn't use the code, and so I posted the error here. @dagem I just wanted to know why you marked ~swim~ answer as the best, when there is no one who should get the best answer. And the answer posted isn't even an answer to the question, rather its for me.
2nd Nov 2017, 9:03 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 6
@ValentinHacker Can you please specify what the code fragment is for (posted in this thread)? Is it for conversion of integer to string? I tried running the code you have posted, but I get nothing as the output. Please tell me what I am doing wrong, or if the code has some other goal to achieve. Thank You. Input: #include<iostream> #include<string> #include<exception> using namespace std; int main() { try { string res = 123+""; cout<<res; } catch(exception& e) { cout<<"Your action produced the "<<e.what()<<"Error."; } } //Ran in CppDroid with -std=c++11 flag.
1st Nov 2017, 10:22 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 5
Yeah. but around here receive error
1st Nov 2017, 7:41 AM
Yanothai Chaitawat
Yanothai Chaitawat - avatar