[lug] [OT] C++ trick?
Mike Romberg
romberg at fsl.noaa.gov
Fri Jan 24 12:05:06 MST 2003
>>>>> " " == Scott Herod <herod at dimensional.com> writes:
> Hello, I'm looking for C++ trick (mainly because it's pretty
> late, and I'm getting tired of sending someone notes that the
> function he's using is not in a standard header file).
> I want to create a macro to replace "itoa( i, buf, size )"
> basically with a function like:
> string f ( int i ) { stringstream b; b << i << ends; return
> b.str(); }
> The following doesn't work because the operator<<() creates
> ostreams which don't know about the str() function of
> stringstreams.
> #define itoa(i,buf,val) \
> (stringstream().operator<<(i).operator<<(ends)).str()
> Can someone suggest a trick?
Not really much of a trick, but how about ostringstream?
#include <sstream>
#include <string>
std::string f(int i)
{
std::ostringstream os;
// Not 100% sure but I think the ends is not needed
// with a stringstream since a string is a vector of chars
os << i << std::ends;
return os.str();
}
Mike Romberg (romberg at fsl.noaa.gov)
More information about the LUG
mailing list