istream_iterator<>

Discussion in 'C++' started by NPC, May 13, 2004.

  1. NPC

    NPC Guest

    Hi,
    Is there any way to use an istream_iterator<> in a way which inserts each
    element at the end of a newline character rather than a space character?
    Could be it looks for any type of whitespace - not sure about that.

    In particular, it would be nice to use it in a way which is similar to an
    ostream_iterator:

    std::vector<std::string> myVec;
    /* add stuff to myVec */

    /* add newline at the end of each element sent to cout */
    std::copy(myVec.begin(), myVec.end(),
    std::eek:stream_iterator<std::string>(std::cout, "\n"));

    // wish I could....
    myVec.clear();
    std::istream_iterator<std::string> myFile("ok.txt", /* cannot do this */,
    "\n");
    std::istream_iterator<std::string> eof;
    std::copy(myFile, eof, std::back_inserter(myVec));


    Anyway to get the same affect using an istream_iterator? Perhaps through a
    traits class? Not interested in a "getline" solution here --> specifically
    looking to use istream_iterator.

    Thanks,
    NPC
     
    NPC, May 13, 2004
    #1
    1. Advertising

  2. NPC

    David Harmon Guest

    On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"
    <> wrote,
    > Is there any way to use an istream_iterator<> in a way which inserts each
    >element at the end of a newline character rather than a space character?
    >Could be it looks for any type of whitespace - not sure about that.


    istream_iterator just uses operator>> for whatever type.



    struct by_line: std::string { };

    std::istream & operator>> (std::istream &is, by_line &to_get)
    {
    std::getline(is, to_get);
    return is;
    }

    int main()
    {
    std::ifstream in("datafile");
    std::vector<std::string> v;
    std::copy( std::istream_iterator<by_line>(in),
    std::istream_iterator<by_line>(),
    std::back_inserter(v));
    }
     
    David Harmon, May 14, 2004
    #2
    1. Advertising

  3. NPC

    NPC Guest

    "David Harmon" <> wrote in message
    news:...
    > On Thu, 13 May 2004 18:54:36 GMT in comp.lang.c++, "NPC"
    > <> wrote,
    > > Is there any way to use an istream_iterator<> in a way which inserts

    each
    > >element at the end of a newline character rather than a space character?
    > >Could be it looks for any type of whitespace - not sure about that.

    >
    > istream_iterator just uses operator>> for whatever type.
    >
    >
    >
    > struct by_line: std::string { };
    >
    > std::istream & operator>> (std::istream &is, by_line &to_get)
    > {
    > std::getline(is, to_get);
    > return is;
    > }
    >
    > int main()
    > {
    > std::ifstream in("datafile");
    > std::vector<std::string> v;
    > std::copy( std::istream_iterator<by_line>(in),
    > std::istream_iterator<by_line>(),
    > std::back_inserter(v));
    > }
    >



    Wow! That's gorgeous! That's been bugging me for way too long. Thank
    you - very, very much David.
     
    NPC, May 14, 2004
    #3
  4. NPC

    tom_usenet Guest

    On Fri, 14 May 2004 09:20:54 GMT, "NPC" <>
    wrote:

    >Wow! That's gorgeous! That's been bugging me for way too long. Thank
    >you - very, very much David.


    An alternative approach is to change the locale's definition of
    whitespace so that operator>> for string does the right thing. That
    approach has been detailed here before - try a google groups search
    for e.g. "comma_ctype" or whitespace ctype facet. Posts by Dietmar
    Kuehl are the ones to read.

    Tom
    --
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
     
    tom_usenet, May 14, 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. Bill Rudolph

    istream_iterator question

    Bill Rudolph, Aug 14, 2003, in forum: C++
    Replies:
    4
    Views:
    551
    Bill Rudolph
    Aug 15, 2003
  2. Chris Mantoulidis
    Replies:
    2
    Views:
    418
  3. Alex Vinokur

    istream_iterator & copying files

    Alex Vinokur, Apr 19, 2004, in forum: C++
    Replies:
    10
    Views:
    1,242
    Alex Vinokur
    Apr 20, 2004
  4. Alex Vinokur

    Copying with istream_iterator

    Alex Vinokur, Jul 21, 2004, in forum: C++
    Replies:
    9
    Views:
    596
    Alex Vinokur
    Jul 24, 2004
  5. ben

    about istream_iterator

    ben, May 17, 2005, in forum: C++
    Replies:
    2
    Views:
    421
Loading...

Share This Page