473,785 Members | 2,484 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ostringstream(s tring) constructor

Hi all,

Why doesn't the following unmanaged C++ code work as expected:

string s;
ostringstream strm(s); // This stream should store results in s
strm << 25;
cout << s << endl; // s still contains an empty string
cout << strm.str(); // but the stream internally contains "25"
Nov 17 '05 #1
3 1402
Bob Altman wrote:
Hi all,

Why doesn't the following unmanaged C++ code work as expected:

string s;
ostringstream strm(s); // This stream should store results in s
Where did you get the idea that the comment above is correct? The above
is just shorthand for:
ostringstream strm;
strm << s;
strm << 25;
cout << s << endl; // s still contains an empty string
Right, since you've misunderstood what the ostringstream constructor does.
cout << strm.str(); // but the stream internally contains "25"


An ostringstream doesn't hold a reference to a string, but rather is
interoperable with strings. You need:

ostringstream strm;
strm << 25;
string s(strm.str());
//etc.

Tom
Nov 17 '05 #2
> > string s;
ostringstream strm(s); // This stream should store results in s


Where did you get the idea that the comment above is correct?


"Beginning C++" by Ivor Horton, 1998 ed., page 802, states (apparently
incorrectly):

You can use an ostringstream object to format data into a string. For
instance, you could create a string object and an output string stream with
the statements:

string outBuffer;
ostringstream outStr(outBuffe r);

You can now use the insertion operators to write to outBuffer via outStr:

double number = 2.5;
outStr << "number = " << (number / 2);

As a result of the write to the string stream, outBuffer will contain
"Number = 1.25"... The string parameter to the string stream constructor is
a reference, so write operations for ostringstream objects act directly on
the string object.
Nov 17 '05 #3
Bob Altman wrote:
string s;
ostringstream strm(s); // This stream should store results in s


Where did you get the idea that the comment above is correct?


"Beginning C++" by Ivor Horton, 1998 ed., page 802, states (apparently
incorrectly):


Dump that book and pick up a copy of "Accelerate d C++" by Andrew Koenig and
Barbara Moo. It's 1/3 the length, has far more content, and is actually
correct.

-cd
Nov 17 '05 #4

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

Similar topics

4
6205
by: Alex Vinokur | last post by:
Is it possible to use vector<ostringstream> ? Here is what I have got. =========================================== Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease) ===========================================
6
3027
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 << "\"\" \"\"" << outfilename << "\"\""; //line 75
3
5609
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
3
11149
by: Kyle Kolander | last post by:
I posted this in response to a previous thread but have not gotten any replies back... Hopefully someone has an answer? From looking at sections 27.7.3.2 and 27.7.1.2 of the standard, it appears str("") does not clear the stream bits. It seems to me that in clearing the internal buffer, one would intend that the stream bits would also be reset (in a good state). Is there a reason this is not the defined behavior? Aside from a slight...
8
1539
by: Bob Smith | last post by:
I am downloading over http port 80 some contents froma site, but the contents is not properly stored after using ostringstream for temporary storage, and later ostringstream::str() for passing it over to a string. This doesn't work. It misses data and teh result is not the same as when using char buffer, which gives correct download. Could anyone point out why the ostringstream + string way doesn't work, while the char buffer does? tia...
3
4081
by: Generic Usenet Account | last post by:
With the deprecated ostrstream class, when the constructor is invoked without arguments, memory is dynamically allocated. In that case the onus on freeing the memory lies with the user. Typically this is done by obtaining the char buffer (by invoking the str() method) and then explicitly deleting it. Does the ostringstream class also have the same issue? I mean, if I instantiate ostringstream without any constructor arguments, is the...
7
2874
by: Thomas Lenz | last post by:
Please consider the following code snippet: string myfunction() { ostringstream oss; oss << "junk"; // do something more with oss; I can't make it const... return oss.str(); } Is the returned string object still valid after myfunction() has returned? I
11
3667
by: coomberjones | last post by:
I have a few std::strings that I am using to store raw binary data, each of which may very well include null bytes at any point or points. I want to slap them together into a single string, so I tried a std::ostringstream: std::ostringstream oss; oss << x << y << z; std::string result ( oss.str() ); The result shows that feeding the ostringstream with a string just
9
3734
by: jl_post | last post by:
Hi, A few months back I remember reading through C++ newsgroups trying to find a way to quickly convert a number to a C++ std::string. I often see code like: // Create a string that holds a number: int num = 7; // for example char tmp; sprintf(tmp, "%d", num);
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10325
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.