Connecting Tech Pros Worldwide Forums | Help | Site Map

istringstream???

bml
Guest
 
Posts: n/a
#1: Jul 22 '05
Could you help and answer my questions of istringstream? Thanks a lot!

1. Reuse an "istringstream"

istringstream ist;
ist.str("This is FIRST test string");

ist.str("This is SECOND test string");
cout << ist.str() << endl;

Is there any problem with the above code?
Is it necessary to add "ist.clear();" between those two "ist.str()"?


2. Sub-string of an "istringstream"

string str1, str2, str3, str4;
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

How to insert the sub-string, "test", directly into str4, without going
through str1, str2, and str3?
How to remove the counterpart of "str1" from "ist" after "ist >> str1;",
"str1" from "ist" after "ist >> str1;" and so on?


3. Usage of skipws

istringstream ist;
ist.str("This is FIRST test string");
ist >> str1 ;
ist >> str2 ;
ist >> str3;
ist >> str4;

ist >> skipws >> str1 ;
ist >> skipws >> str2 ;
ist >> skipws >> str3;
ist >> skipws >> str4;

Adding "skipws" don't change format and content of str1,...str4 at all? So
what's the usage of "skipws"?


4. Why need ostringstream and istringstream separate, instead of combining
them into a single one with both ">>" and "<<"?





Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 22 '05

re: istringstream???


"bml" <lee4rai@yahoo.com> wrote...[color=blue]
> Could you help and answer my questions of istringstream? Thanks a lot!
>
> 1. Reuse an "istringstream"
>
> istringstream ist;
> ist.str("This is FIRST test string");
>
> ist.str("This is SECOND test string");
> cout << ist.str() << endl;
>
> Is there any problem with the above code?[/color]

Aside from it being a fragment outside of any function, no.
[color=blue]
> Is it necessary to add "ist.clear();" between those two "ist.str()"?[/color]

You mean, between those two "ist.str( <some string literal> )"? No.
[color=blue]
> 2. Sub-string of an "istringstream"
>
> string str1, str2, str3, str4;
> ist >> str1 ;
> ist >> str2 ;
> ist >> str3;
> ist >> str4;
>
> How to insert the sub-string, "test", directly into str4, without going
> through str1, str2, and str3?[/color]

There is no way. It's a stream. Well, not entirely true, you could
of course, position the sream if you knew where.
[color=blue]
> How to remove the counterpart of "str1" from "ist" after "ist >> str1;",
> "str1" from "ist" after "ist >> str1;" and so on?[/color]

What do you mean by "remove"? The stream [buffer] position is changed
after every reading. Why do you need to remove anything?
[color=blue]
> 3. Usage of skipws
>
> istringstream ist;
> ist.str("This is FIRST test string");
> ist >> str1 ;
> ist >> str2 ;
> ist >> str3;
> ist >> str4;
>
> ist >> skipws >> str1 ;
> ist >> skipws >> str2 ;
> ist >> skipws >> str3;
> ist >> skipws >> str4;
>
> Adding "skipws" don't change format and content of str1,...str4 at all? So
> what's the usage of "skipws"?[/color]

IIRC, it affects only some operations. For example, if you read 'str1' and
then try to extract the _rest_ of the buffer, youi will get "is FIRST..."
instead of " is FIRST...".
[color=blue]
> 4. Why need ostringstream and istringstream separate, instead of combining
> them into a single one with both ">>" and "<<"?[/color]

I guess I don't understand the question. There is 'std::stringstream',
which is bi-directional. Why have std::ostream and std::istream?..

V


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

re: istringstream???


Victor Bazarov <v.Abazarov@comAcast.net> wrote in message
news:FInRb.161776$xy6.780699@attbi_s02...[color=blue]
> "bml" <lee4rai@yahoo.com> wrote...[color=green]
> > Could you help and answer my questions of istringstream? Thanks a lot!
> >
> > 1. Reuse an "istringstream"
> >
> > istringstream ist;
> > ist.str("This is FIRST test string");
> >
> > ist.str("This is SECOND test string");
> > cout << ist.str() << endl;
> >
> > Is there any problem with the above code?[/color]
>
> Aside from it being a fragment outside of any function, no.
>[color=green]
> > Is it necessary to add "ist.clear();" between those two "ist.str()"?[/color]
>[/color]

Thank you for your answers! So what's the usage of ist.clear()?

[color=blue]
> You mean, between those two "ist.str( <some string literal> )"? No.
>[color=green]
> > 2. Sub-string of an "istringstream"
> >
> > string str1, str2, str3, str4;
> > ist >> str1 ;
> > ist >> str2 ;
> > ist >> str3;
> > ist >> str4;
> >
> > How to insert the sub-string, "test", directly into str4, without going
> > through str1, str2, and str3?[/color]
>
> There is no way. It's a stream. Well, not entirely true, you could
> of course, position the sream if you knew where.
>[/color]

What do you mean by positioning the stream?

[color=blue][color=green]
> > 4. Why need ostringstream and istringstream separate, instead of[/color][/color]
combining[color=blue][color=green]
> > them into a single one with both ">>" and "<<"?[/color]
>
> I guess I don't understand the question. There is 'std::stringstream',
> which is bi-directional. Why have std::ostream and std::istream?..
>[/color]
Exactly is it what I wanted to ask.



Karl Heinz Buchegger
Guest
 
Posts: n/a
#4: Jul 22 '05

re: istringstream???


bml wrote:[color=blue]
>[color=green][color=darkred]
> > > 2. Sub-string of an "istringstream"
> > >
> > > string str1, str2, str3, str4;
> > > ist >> str1 ;
> > > ist >> str2 ;
> > > ist >> str3;
> > > ist >> str4;
> > >
> > > How to insert the sub-string, "test", directly into str4, without going
> > > through str1, str2, and str3?[/color]
> >
> > There is no way. It's a stream. Well, not entirely true, you could
> > of course, position the sream if you knew where.
> >[/color]
>
> What do you mean by positioning the stream?[/color]

How old are you? I don't ask because I want to be impolite
or I want to insult you BUT I just want to know if you are old
enough to remember the days of audio casette recorders or tape
drives.

A stream works like a tape. You can only read and write sequentially.
Well. If you know exactly what you are looking for you can press the[color=blue][color=green]
>> button on your tape deck and do a fast forward. But you need to[/color][/color]
be carefull, without a tape meter it is practically impossible to
position the tape *exactly* where you want to start. And even then it
is impossible if you don't know the length of each song on the tape.
With a stream its similar: If you know the exact location you can
position the stream there and start reading. But without this knowledge
you are lost. So if there are 4 words in the stream and you do not know
anything about them such as the length of each, there simply is no way to
determine the start of the 4-th word other then read through the previous
words.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Closed Thread