471,582 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,582 software developers and data experts.

istringstream???

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


Jul 22 '05 #1
3 4547
"bml" <le*****@yahoo.com> wrote...
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, between those two "ist.str( <some string literal> )"? No.
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::ostream and std::istream?..

V
Jul 22 '05 #2
bml
Victor Bazarov <v.********@comAcast.net> wrote in message
news:FInRb.161776$xy6.780699@attbi_s02...
"bml" <le*****@yahoo.com> wrote...
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()"?


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

You mean, between those two "ist.str( <some string literal> )"? No.
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.


What do you mean by positioning the stream?

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::ostream and std::istream?..

Exactly is it what I wanted to ask.

Jul 22 '05 #3
bml wrote:
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.


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 the
button on your tape deck and do a fast forward. But you need to

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
kb******@gascad.at
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Samuele Armondi | last post: by
8 posts views Thread by Agent Mulder | last post: by
7 posts views Thread by Luther Baker | last post: by
4 posts views Thread by Brandon | last post: by
6 posts views Thread by JustSomeGuy | last post: by
8 posts views Thread by Randy Yates | last post: by
6 posts views Thread by James Aguilar | last post: by
11 posts views Thread by icanoop | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by

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.