473,385 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

std::ostringstream

bob
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

G

Feb 21 '06 #1
2 3516
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.
Feb 21 '06 #2
bob
Genius! I've been looking for a reason to use boost for a while
now....this is ideal. :)

cheers mate,

G

Feb 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\"...
3
by: Chris | last post by:
Hi, I'm playing/developing a simple p2p file sharing client for an existing protocol. A 2 second description of the protocol is Message Length = int, 4 bytes Message Code = int, 4 bytes...
6
by: Christopher Benson-Manica | last post by:
Is there anything that one can do to a std::ostringstream that would make its destructor explode? I'm basically doing { std::ostringstream ss, msg; // do BUNCHES of stuff with ss and msg ...
5
by: Simon Pryor | last post by:
I am having some strange problems using std::ostringstream. The simple stuff works okay, but trying to use: std::ostringstream::str(const std::string&) or:...
1
by: Jason Heyes | last post by:
I would like to have std::cout redirect its output to a std::ostringstream. Can this be done? Help is appreciated.
1
by: Voronkov Konstantin | last post by:
Hello all! std::ostringstream stream; stream << 8080; std::string str = stream.str(); // str == "8 080" The code shown above in mine big program result the str variable value to
4
by: Olaf van der Spek | last post by:
Hi, I'm using ostringstream and I'd like to generate \r\n line ends instead of \n ones. Is there a flag for this? Or should I just insert the \r myself?
25
by: Bala2508 | last post by:
Hi, I have a C++ application that extensively uses std::string and std::ostringstream in somewhat similar manner as below std::string msgHeader; msgHeader = "<"; msgHeader += a; msgHeader...
2
by: mathieu | last post by:
Hi, I am playing with the following C++ piece of code (*). At least on my system debian/gcc 4.3 it looks like I am not writing out a floating point separator as a comma. what are the operation...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.