STL Alternatives to itoa, ltoa, strtol, and strtod

Submitted by date of submission user level 

Bulent Ozkir April 04, 2001 Beginner 

You can use the following generic template functions to convert Numbers to Strings using STL stringstreams... 

 

 template 

inline basic_string toTString(const T& t) {

 basic_ostringstream o;

 o << t;

 return o.str();

}

template 

inline string toString(const T& t) {

 return toTString(t);

}

template 

inline wstring toWString(const T& t) {

 return toTString(t);

  

  

sample ANSI usage: 

// use toWString for UNICODE... 

float f = 5.5; 

string s = toString (f); 

cout << s << endl; 

   

 

 

 

--------------------------------------------------------------------------------

Bulent Ozkir is a Turkey based senior software engineer. He has worked as a senior support engineer for Microsoft from 1998 to 2000. In his over 5 years programming career he has been working on various programming technologies including VB, VC, Visual Interdev, JavaScript, ASP, HTML/DHTML, WMI, COM+, TCP/IP, IIS, SS3, MCIS25, ASP and SQL Server. His background includes B.A. Comp. Sc., MCSE + Internet and MCSD. See members area for more details.