copy an istringstream

Discussion in 'C++' started by dover, Jul 7, 2004.

  1. dover

    dover Guest

    /*Copy the line a token at a time into the output*/
    copy(istream_iterator<string>(iss),
    istream_iterator<string>(),
    ostream_iterator<string>(oss, " "));

    What's the meaning of this statement? Thanks!
     
    dover, Jul 7, 2004
    #1
    1. Advertising

  2. dover

    Sharad Kala Guest

    "dover" <> wrote in message
    news:g1OGc.207220$...
    > /*Copy the line a token at a time into the output*/
    > copy(istream_iterator<string>(iss),
    > istream_iterator<string>(),
    > ostream_iterator<string>(oss, " "));
    >
    > What's the meaning of this statement? Thanks!


    Again as your previous post the comment gives you the answer. copy algorithm
    takes three parameters - the first two are beginning and one past the end
    iterators of the source range and the third parameter is the beginning
    iterator of the destination range.
    So your source range is a istringstream (strings can behave as external
    devices to streams.) Your destination is an ostringstream with space as the
    delimiter.
    So if input stream contains "This is a test" then destination string will
    also contain "This is a test".

    -Sharad
     
    Sharad Kala, Jul 7, 2004
    #2
    1. Advertising

  3. dover

    Rolf Magnus Guest

    dover wrote:

    > /*Copy the line a token at a time into the output*/
    > copy(istream_iterator<string>(iss),
    > istream_iterator<string>(),
    > ostream_iterator<string>(oss, " "));
    >
    > What's the meaning of this statement? Thanks!


    It copies the line a token at a time into the output.
     
    Rolf Magnus, Jul 7, 2004
    #3
  4. dover wrote:
    > /*Copy the line a token at a time into the output*/
    > copy(istream_iterator<string>(iss),
    > istream_iterator<string>(),
    > ostream_iterator<string>(oss, " "));
    >
    > What's the meaning of this statement? Thanks!


    Others have answered this question. I'd like to pose a bonus question
    to the group: what's the meaning of this statement?

    vector<string> vec( istream_iterator<string>(iss),
    istream_iterator<string>() );

    --
    Russell Hanneken

    Use ROT13 to decode my email address.
     
    Russell Hanneken, Jul 7, 2004
    #4
  5. dover

    Sharad Kala Guest

    "Russell Hanneken" <> wrote in message
    news:_jWGc.7781$...
    > dover wrote:
    > > /*Copy the line a token at a time into the output*/
    > > copy(istream_iterator<string>(iss),
    > > istream_iterator<string>(),
    > > ostream_iterator<string>(oss, " "));
    > >
    > > What's the meaning of this statement? Thanks!

    >
    > Others have answered this question. I'd like to pose a bonus question
    > to the group: what's the meaning of this statement?
    >
    > vector<string> vec( istream_iterator<string>(iss),
    > istream_iterator<string>() );


    C++'s most vexing parse! It's a function declaration and not that of a
    vector. To get correct results put another set of parenthesis -
    vector<string> vec( (istream_iterator<string>(iss)),
    istream_iterator<string>() );

    Seems you have read Effective STL ;-)

    -Sharad
     
    Sharad Kala, Jul 8, 2004
    #5
  6. dover

    Sharad Kala Guest

    "Russell Hanneken" <> wrote in message
    news:_jWGc.7781$...
    > dover wrote:
    > > /*Copy the line a token at a time into the output*/
    > > copy(istream_iterator<string>(iss),
    > > istream_iterator<string>(),
    > > ostream_iterator<string>(oss, " "));
    > >
    > > What's the meaning of this statement? Thanks!

    >
    > Others have answered this question. I'd like to pose a bonus question
    > to the group: what's the meaning of this statement?
    >
    > vector<string> vec( istream_iterator<string>(iss),
    > istream_iterator<string>() );


    C++'s most vexing parse! It's a function declaration and not that of a
    vector. To get correct results put another set of parenthesis -
    vector<string> vec( (istream_iterator<string>(iss)),
    istream_iterator<string>() );

    Seems you have read Effective STL ;-)

    -Sharad
     
    Sharad Kala, Jul 8, 2004
    #6
  7. Sharad Kala wrote:
    > "Russell Hanneken" <> wrote in message
    > news:_jWGc.7781$...
    >
    >>I'd like to pose a bonus question
    >>to the group: what's the meaning of this statement?
    >>
    >> vector<string> vec( istream_iterator<string>(iss),
    >> istream_iterator<string>() );

    >
    > C++'s most vexing parse! It's a function declaration and not that of a
    > vector. To get correct results put another set of parenthesis -
    > vector<string> vec( (istream_iterator<string>(iss)),
    > istream_iterator<string>() );
    >
    > Seems you have read Effective STL ;-)


    Aw, you got it right. That's no fun. :^)

    I haven't actually read _Effective STL_, but I did read "Guru of the
    Week" #75, which covers the issue:

    http://www.gotw.ca/gotw/075.htm

    If anyone's confused about why the statement above declares a function
    rather than a vector, I recommend the article.

    --
    Russell Hanneken

    Use ROT13 to decode my email address.
     
    Russell Hanneken, Jul 8, 2004
    #7
  8. dover

    Alex Vinokur Guest

    "Sharad Kala" <> wrote in message news:...
    >
    > "Russell Hanneken" <> wrote in message
    > news:_jWGc.7781$...
    > > dover wrote:
    > > > /*Copy the line a token at a time into the output*/
    > > > copy(istream_iterator<string>(iss),
    > > > istream_iterator<string>(),
    > > > ostream_iterator<string>(oss, " "));
    > > >
    > > > What's the meaning of this statement? Thanks!

    > >
    > > Others have answered this question. I'd like to pose a bonus question
    > > to the group: what's the meaning of this statement?
    > >
    > > vector<string> vec( istream_iterator<string>(iss),
    > > istream_iterator<string>() );

    >
    > C++'s most vexing parse! It's a function declaration and not that of a vector.


    Is it really a function _declaration_? : iss is not type, but variable (istringstream iss).

    > To get correct results put another set of parenthesis -
    > vector<string> vec( (istream_iterator<string>(iss)),
    > istream_iterator<string>() );

    [snip]


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jul 8, 2004
    #8
  9. dover

    Sharad Kala Guest

    "Alex Vinokur" <> wrote in message
    news:...
    >
    > "Sharad Kala" <> wrote in message

    news:...
    > >
    > > "Russell Hanneken" <> wrote in message
    > > news:_jWGc.7781$...


    > Is it really a function _declaration_? : iss is not type, but variable

    (istringstream iss).

    Yes, it is.

    void foo( int d);
    void foo (int (d)); // same as above; parens around d are ignored

    With this knowledge, in vector<string> vec( istream_iterator<string>(iss),
    istream_iterator<string>() );
    the first parameter is of type istream_iterator<string>. The parentheses
    around iss are superfluous and are ignored. I hope you have got the second
    parameter right by yourself :)

    -Sharad
     
    Sharad Kala, Jul 8, 2004
    #9
  10. dover

    Alex Vinokur Guest

    "Sharad Kala" <> wrote in message news:...
    [snip]
    >
    > void foo( int d);
    > void foo (int (d)); // same as above; parens around d are ignored


    d is of int type

    >
    > With this knowledge, in vector<string> vec( istream_iterator<string>(iss),
    > istream_iterator<string>() );
    > the first parameter is of type istream_iterator<string>. The parentheses
    > around iss are superfluous and are ignored.

    [snip]

    But iss is not of istream_iterator<string> type; iss is of istringstream type.


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jul 8, 2004
    #10
  11. dover

    Sharad Kala Guest

    "Alex Vinokur" <> wrote in message
    news:...
    >
    > "Sharad Kala" <> wrote in message

    news:...

    >
    > But iss is not of istream_iterator<string> type; iss is of istringstream

    type.

    So? You think this won't compile?

    float f;
    void foo (int (f)); // ==> void foo (int f);

    -Sharad
     
    Sharad Kala, Jul 8, 2004
    #11
  12. dover

    tom_usenet Guest

    On Thu, 8 Jul 2004 14:09:27 +0300, "Alex Vinokur"
    <> wrote:

    >
    >"Sharad Kala" <> wrote in message news:...
    >[snip]
    >>
    >> void foo( int d);
    >> void foo (int (d)); // same as above; parens around d are ignored

    >
    >d is of int type
    >
    >>
    >> With this knowledge, in vector<string> vec( istream_iterator<string>(iss),
    >> istream_iterator<string>() );
    >> the first parameter is of type istream_iterator<string>. The parentheses
    >> around iss are superfluous and are ignored.

    >[snip]
    >
    >But iss is not of istream_iterator<string> type; iss is of istringstream type.


    Function declaration scope iss hides the prior one. This is perfectly
    legal:

    double d;
    int f(int(d)); //function declaration, parameter named d.

    Tom
    --
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, Jul 8, 2004
    #12
  13. dover

    Alex Vinokur Guest

    Function declaration (was: copy an istringstream)

    "Sharad Kala" <> wrote in message news:...
    >
    > "Alex Vinokur" <> wrote in message
    > news:...
    > >
    > > "Sharad Kala" <> wrote in message

    > news:...
    >
    > >
    > > But iss is not of istream_iterator<string> type; iss is of istringstream

    > type.
    >
    > So? You think this won't compile?
    >
    > float f;
    > void foo (int (f)); // ==> void foo (int f);
    >
    > -Sharad
    >
    >


    OK

    Now is some problem with second parameter in foo2().

    ------ foo.cpp ------
    void foo1 (int (i), int) {}
    void foo2 (int (i), int()) {}
    int main()
    {
    foo1 (10, 20); // Line#5
    foo2 (30, 40); // Line#6
    return 0;
    }
    ---------------------

    ------ Compilation ------
    $ g++ foo.cpp
    foo.cpp: In function `int main()':
    foo.cpp:6: error: invalid conversion from `int' to `int (*)()'
    -------------------------


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jul 8, 2004
    #13
  14. dover

    Sharad Kala Guest

    Re: Function declaration (was: copy an istringstream)

    "Alex Vinokur" <> wrote in message
    news:...
    >
    > "Sharad Kala" <> wrote in message

    news:...
    > >
    > > "Alex Vinokur" <> wrote in message
    > > news:...
    > > >
    > > > "Sharad Kala" <> wrote in message

    > > news:...
    > >
    > > >
    > > > But iss is not of istream_iterator<string> type; iss is of

    istringstream
    > > type.
    > >
    > > So? You think this won't compile?
    > >
    > > float f;
    > > void foo (int (f)); // ==> void foo (int f);
    > >
    > > -Sharad
    > >
    > >

    >
    > OK
    >
    > Now is some problem with second parameter in foo2().
    >
    > ------ foo.cpp ------
    > void foo1 (int (i), int) {}



    > void foo2 (int (i), int()) {}


    This declares a function, foo2, whose return type is void. It takes two
    parameters -
    - The first parameter is names, its type is an int
    - The second parameter has NO NAME. It's type is pointer to function taking
    nothing and returning int as parameter.

    Probably what you are missing is that parentheses around a parameter name
    are ignored (as is in the case in our first parameter), but parentheses
    standing by themselves (as is in our second parameter), indicate the
    existence of a parameter list. They indicate the presence of a parameter
    that is
    itself a pointer to a function.

    > int main()
    > {
    > foo1 (10, 20); // Line#5
    > foo2 (30, 40); // Line#6
    > return 0;
    > }
    > ---------------------
    >
    > ------ Compilation ------
    > $ g++ foo.cpp
    > foo.cpp: In function `int main()':
    > foo.cpp:6: error: invalid conversion from `int' to `int (*)()'


    The compiler error message should also give you the answer!

    Hope that clears it.

    -Sharad
     
    Sharad Kala, Jul 9, 2004
    #14
  15. dover

    Alex Vinokur Guest

    Re: Function declaration (was: copy an istringstream)

    "Sharad Kala" <> wrote in message news:...
    >
    > "Alex Vinokur" <> wrote in message
    > news:...

    [snip]
    > >
    > > Now is some problem with second parameter in foo2().
    > >
    > > ------ foo.cpp ------
    > > void foo1 (int (i), int) {}

    >
    >
    > > void foo2 (int (i), int()) {}

    >
    > This declares a function, foo2, whose return type is void. It takes two
    > parameters -
    > - The first parameter is names, its type is an int
    > - The second parameter has NO NAME. It's type is pointer to function taking
    > nothing and returning int as parameter.
    >
    > Probably what you are missing is that parentheses around a parameter name
    > are ignored (as is in the case in our first parameter), but parentheses
    > standing by themselves (as is in our second parameter), indicate the
    > existence of a parameter list. They indicate the presence of a parameter
    > that is
    > itself a pointer to a function.
    >
    > > int main()
    > > {
    > > foo1 (10, 20); // Line#5
    > > foo2 (30, 40); // Line#6
    > > return 0;
    > > }
    > > ---------------------
    > >
    > > ------ Compilation ------
    > > $ g++ foo.cpp
    > > foo.cpp: In function `int main()':
    > > foo.cpp:6: error: invalid conversion from `int' to `int (*)()'

    >
    > The compiler error message should also give you the answer!
    >
    > Hope that clears it.
    >
    > -Sharad
    >
    >


    Thanks.

    Unfortunately C++ doesn't require void in function declaration.
    It seems that void foo2 (int (i), int(void)) is more intuitive declaration.


    --
    Alex Vinokur
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Jul 9, 2004
    #15
    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:
    369
    Samuele Armondi
    Jul 11, 2003
  2. Agent Mulder

    istringstream to bool

    Agent Mulder, Aug 23, 2003, in forum: C++
    Replies:
    8
    Views:
    1,018
    Agent Mulder
    Aug 24, 2003
  3. Alex
    Replies:
    2
    Views:
    1,242
  4. mathieu
    Replies:
    7
    Views:
    409
    mathieu
    Jul 24, 2006
  5. Replies:
    26
    Views:
    2,129
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page