[LC++]strings and numbers

Mark Phillips mark at austrics.com.au
Thu Jul 4 12:12:05 UTC 2002


Peter Poulsen wrote:

> Hi (again)
> 
> I'm getting a little tired of using atoi and sprintf to convert between 
> numbers and strings. I'm using STL strings, and I was thinking that 
> perhaps it have some build in methods that can do the trick smothly?
> 


The best conversion routines I have been able to come up with are
as follows:


   #include <sstream>
   #include <string>

   template<typename NumTv>
   inline void toString(std::string& to, NumTv num) {
     ostringstream ossm;
     ossm<<num;
     to=ossm.str();
   }

   template<typename NumTv>
   inline std::string toString(NumTv num) {
     ostringstream ossm;
     ossm<<num;
     return ossm.str();
   }

   template<typename NumTv>
   inline bool fromString(NumTv& num, std::string const& from) {
     istringstream iss(from);
     char testChar;
     return ((iss>>num)&&!(iss>>testChar));
   }

   template<typename NumTv>
   inline NumTv fromString(std::string const& from) {
     istringstream iss(from);
     NumTv ret;
     iss>>ret;
     return ret;
   }

Going from a number to a string doesn't require error checking
(I don't think it does anyway), whereas going from a string
to a number does.  For that reason the first of the "fromString"
routines is the more safe as it returns a bool value describing
whether the operation was successful.

The only thing I don't like about the toString solution is that
it involves the creation of a temporary string as part of the
ostringstream structure, and copies this to the return string.
I wish there were a way of saying something like

   ostringstream ossm(to);

which meant "use the string 'to' as the internal string buffer".
This would make for a more efficient conversion routine.  Of
course I realize there are problems with this kind of constructor,
but perhaps this is a good reason for the language standard to
include such conversion routines natively??

Anyway, here is some test code for the above, along with its
output:

   cout<<"\n\nabout to test conversions\n\n"<<flush;
   {
     int i=4; int j;
     double d=6.243; double e;
     string s; string t;
     toString(s, i); toString(t, d);
     cout<<"s==\""<<s<<"\" and t==\""<<t<<"\"\n"<<flush;
     if (!fromString(j, "-334"))
       cout<<"\nconversion error with \"-334\"\n"<<flush;
     if (!fromString(e, "-9.61"))
       cout<<"\nconversion error with \"-9.61\"\n"<<flush;
     cout<<"j=="<<j<<" and e=="<<e<<endl;
     if (!fromString(j, "55534.6"))
       cout<<"\nconversion error with \"55534.6\"\n"<<flush;
     if (!fromString(e, "-9.61banana"))
       cout<<"\nconversion error with \"-9.61banana\"\n"<<flush;
     cout<<"j=="<<j<<" and e=="<<e<<endl;
   }

about to test conversions

s=="4" and t=="6.243"
j==-334 and e==-9.61

conversion error with "55534.6"

conversion error with "-9.61banana"
j==55534 and e==-9.61


Cheers,

Mark.

-- 
Dr Mark H Phillips
Research Analyst (Mathematician)

AUSTRICS - smarter scheduling solutions - www.austrics.com

Level 2, 50 Pirie Street, Adelaide SA 5000, Australia
Phone +61 8 8226 9850
Fax   +61 8 8231 4821
Email mark at austrics.com.au





More information about the tuxCPProgramming mailing list