istringstream???

Discussion in 'C++' started by bml, Jan 27, 2004.

  1. bml

    bml Guest

    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 "<<"?
    bml, Jan 27, 2004
    #1
    1. Advertising

  2. "bml" <> 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::eek:stream and std::istream?..

    V
    Victor Bazarov, Jan 27, 2004
    #2
    1. Advertising

  3. bml

    bml Guest

    Victor Bazarov <> wrote in message
    news:FInRb.161776$xy6.780699@attbi_s02...
    > "bml" <> 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::eek:stream and std::istream?..
    >

    Exactly is it what I wanted to ask.
    bml, Jan 27, 2004
    #3
  4. 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
    Karl Heinz Buchegger, Jan 27, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuele Armondi

    Allocating istringstream objects

    Samuele Armondi, Jul 11, 2003, in forum: C++
    Replies:
    1
    Views:
    359
    Samuele Armondi
    Jul 11, 2003
  2. Agent Mulder

    istringstream to bool

    Agent Mulder, Aug 23, 2003, in forum: C++
    Replies:
    8
    Views:
    987
    Agent Mulder
    Aug 24, 2003
  3. Viet Le Hong

    istringstream question

    Viet Le Hong, Sep 18, 2003, in forum: C++
    Replies:
    3
    Views:
    651
    Buster Copley
    Sep 18, 2003
  4. Donald Canton

    istringstream syntax error question

    Donald Canton, Jan 29, 2004, in forum: C++
    Replies:
    1
    Views:
    399
    David Harmon
    Jan 30, 2004
  5. Luther Baker

    istringstream question

    Luther Baker, May 22, 2004, in forum: C++
    Replies:
    7
    Views:
    3,843
    John Harrison
    May 22, 2004
Loading...

Share This Page