ostringstream

B

bml

For the code below:

ostringstream oss;
oss << "A string using ostreamstring" << endl;
oss.str("");
oss.clear();

"'endl' adds a newline character to and flushes the stream of oss."
What does it mean by "flushes the stream"?

Is there any difference to clear a string content of oss by oss.str("") and
by oss.clear()?

How to remove first 10 bytes from the oss, for the above example, causing
the string content of oss to be "sing ostreamstring"?

How to find the position of "using" inside oss? Say, "using" has a byte
position of 9 for example.

Thanks a lot!
 
J

John Harrison

bml said:
For the code below:

ostringstream oss;
oss << "A string using ostreamstring" << endl;
oss.str("");
oss.clear();

"'endl' adds a newline character to and flushes the stream of oss."
What does it mean by "flushes the stream"?

It means nothing, ostringstream's do not flush.
Is there any difference to clear a string content of oss by oss.str("") and
by oss.clear()?

oss.clear() does not clear a string content, so yes there is a difference!

clear(), clears the error state of a stream, it has nothing to do with
content. RTFM I think.
How to remove first 10 bytes from the oss, for the above example, causing
the string content of oss to be "sing ostreamstring"?

That cannot be done, any more than you can remove the first 10 bytes from a
file. If you really need this then

1) get the string from the stringstream
2) remove the first 10 bytes from the string
3) put the string back in the stringstream
How to find the position of "using" inside oss? Say, "using" has a byte
position of 9 for example.

Again this sounds like a string operation. Seems you are using stringstreams
when you should be using strings.
Thanks a lot!

John
 
R

Rolf Magnus

bml said:
For the code below:

ostringstream oss;
oss << "A string using ostreamstring" << endl;
oss.str("");
oss.clear();

"'endl' adds a newline character to and flushes the stream of oss."
What does it mean by "flushes the stream"?

It moves the data from the buffer to its target. That doesn't really
have a meaning for stringstreams.
Is there any difference to clear a string content of oss by
oss.str("") and by oss.clear()?

Yes. One acutally makes the string empty, the other doesn't. clear()
clears the status bits of the stream.
How to remove first 10 bytes from the oss, for the above example,
causing the string content of oss to be "sing ostreamstring"?
oss.str(oss.str().substr(10));

How to find the position of "using" inside oss? Say, "using" has a
byte position of 9 for example.

string::size_type index = oss.str().find("using");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top