Connecting Tech Pros Worldwide Forums | Help | Site Map

ostringstream

bml
Guest
 
Posts: n/a
#1: Jul 22 '05
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!




John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: ostringstream



"bml" <lee4rai@yahoo.com> wrote in message
news:D0hTb.44905$6O4.1316023@bgtnsc04-news.ops.worldnet.att.net...[color=blue]
> 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"?[/color]

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

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.
[color=blue]
>
> How to remove first 10 bytes from the oss, for the above example, causing
> the string content of oss to be "sing ostreamstring"?[/color]

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
[color=blue]
>
> How to find the position of "using" inside oss? Say, "using" has a byte
> position of 9 for example.[/color]

Again this sounds like a string operation. Seems you are using stringstreams
when you should be using strings.
[color=blue]
>
> Thanks a lot!
>
>[/color]

John


Rolf Magnus
Guest
 
Posts: n/a
#3: Jul 22 '05

re: ostringstream


bml wrote:
[color=blue]
> 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"?[/color]

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

Yes. One acutally makes the string empty, the other doesn't. clear()
clears the status bits of the stream.
[color=blue]
> How to remove first 10 bytes from the oss, for the above example,
> causing the string content of oss to be "sing ostreamstring"?[/color]

oss.str(oss.str().substr(10));
[color=blue]
> How to find the position of "using" inside oss? Say, "using" has a
> byte position of 9 for example.[/color]

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

Closed Thread