473,769 Members | 6,807 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 1400
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
3026
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
5608
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
11147
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
4079
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
3665
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
3733
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
9589
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
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9999
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
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
7413
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
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.