Current post is on how to convert basic data type into string in C++ \ VC++
Till now, I used to call 'itoa()' or similar function after which I used to append the char* buffer to string variable.
Lots of temp variable to initalize, worry about charbuff limited size declaration and ugly looking code. I came across this simple and clean function calls which really pleased my eyes :)
Let us take int to string conversion example:
BEFORE:
int myint=0;
char mycharbuf[64]="";
string mystr;
... // Get the int value
itoa(mycharbuff,myint,10);
mystr.append(mycharbuff);
... // Use the string
NOW:
int myint;
string mystr;
stringstream mystream;
... //Get the int value
mystream << myint;
mystr = mystream.str();
... // Use the string
Here you go, with clean code & faster code review of your work.
Appreciate your feedback via comments. Thanks.
No comments:
Post a Comment