istringstream???

B

bml

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 "<<"?
 
V

Victor Bazarov

bml said:
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?

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

You mean said:
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?

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

What do you mean by "remove"? The stream [buffer] position is changed
after every reading. Why do you need to remove anything?
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"?

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...".
4. Why need ostringstream and istringstream separate, instead of combining
them into a single one with both ">>" and "<<"?

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

V
 
B

bml

Victor Bazarov said:
Aside from it being a fragment outside of any function, no.

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

There is no way. It's a stream. Well, not entirely true, you could
of course, position the sream if you knew where.

What do you mean by positioning the stream?

I guess I don't understand the question. There is 'std::stringstream',
which is bi-directional. Why have std::eek:stream and std::istream?..
Exactly is it what I wanted to ask.
 
K

Karl Heinz Buchegger

bml said:
What do you mean by positioning the stream?

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 thebe 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.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top