In article <11**********************@g43g2000cwa.googlegroups .com>,
"bo*@blah.com" <Gr**********@gmail.com> wrote:
For reasons that are irrelevant , we cannot use sprintf to do
conversions between doubles, ints etc. to char*. We're using the
std::ostringstream type. Basically we have a function that takes a
double and needs to store a string representation of it into a vector
of strings. ...
something like this;
void pussy::(const double d)
{
std::ostringstream strm;
strm << d;
stringbuf *pbuf = strm.rdbuf();
_vect[some_index] = pbuf->str();
}
however this code involves taking a copy of the contents of pbuf string
and I'd like to avoid that. Is there a more efficient way of achieving
my goal without using sprintf etc.
thanks
Have you looked into boost.org "lexical_cast"? I personally feel that it
is the best (it even looks like a standard C++ cast.)
string s = lexical_cast<string>( myDouble );
Below is a naive implementation of it, I expect that Boost's full
implementation is faster.
template < typename Result, Input >
Result lexical_cast( Input in ) {
stringstream ss;
Result result;
if ( !( ss << in && ss >> result ) ) throw std::bad_cast();
return result;
}
--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.