several input-output questions

Discussion in 'C++' started by Jess, Jun 17, 2007.

  1. Jess

    Jess Guest

    Hello,

    When I copy contents from an input string to a vector, I typically use
    something like this

    vector<string> v;
    ifstream in("file");
    copy(istream_iterator<string> (in), istream_iterator<string>(),
    back_inserter(v));

    However, I also tried to define istream_iterator separately. I did

    istream_iterator<string> it(in);

    this works, but there's no way I can define something equivalent to
    istream_iterator<string>() above. I tried

    istream_iterator<string> it;

    and this failed. What does "istream_iterator<string>()" mean? Does
    it define an iterator that's not bound to any istream? If so, why my
    code above "istream_iterator<string> it;" doesn't work?

    There are some error-detection functions, such as "bad(), "good()",
    "fail()". I'm not sure what constitutes "errors". If I read from a
    file and hit the end of the file, does it leave the stream in an error
    state? If the reading reaches EOF, then do I need to use "clear()" to
    reset it? If I use "clear()" to reset a stream (for whatever error
    reasons), are the remaining data on the stream lost?

    Thanks a lot!
    Jess
     
    Jess, Jun 17, 2007
    #1
    1. Advertising

  2. Jess wrote:
    > Hello,
    >
    > When I copy contents from an input string to a vector, I typically use
    > something like this
    >
    > vector<string> v;
    > ifstream in("file");
    > copy(istream_iterator<string> (in), istream_iterator<string>(),
    > back_inserter(v));
    >
    > However, I also tried to define istream_iterator separately. I did
    >
    > istream_iterator<string> it(in);
    >
    > this works, but there's no way I can define something equivalent to
    > istream_iterator<string>() above. I tried
    >
    > istream_iterator<string> it;
    >
    > and this failed.


    It should work (apart from the fact that you seem to have used the
    variable name 'it' twice). What error did you get?

    What does "istream_iterator<string>()" mean? Does
    > it define an iterator that's not bound to any istream? If so, why my
    > code above "istream_iterator<string> it;" doesn't work?
    >
    > There are some error-detection functions, such as "bad(), "good()",
    > "fail()". I'm not sure what constitutes "errors". If I read from a
    > file and hit the end of the file, does it leave the stream in an error
    > state?


    Yes

    If the reading reaches EOF, then do I need to use "clear()" to
    > reset it?


    Yes, but be careful here, reaching the end of file is not the same as
    trying to read past the end of file. Only the latter is an error.

    If I use "clear()" to reset a stream (for whatever error
    > reasons), are the remaining data on the stream lost?


    No

    >
    > Thanks a lot!
    > Jess
    >
     
    John Harrison, Jun 17, 2007
    #2
    1. Advertising

  3. Jess

    Jess Guest

    On Jun 17, 11:16 pm, John Harrison <>
    wrote:
    > Jess wrote:
    > > Hello,

    >
    > > When I copy contents from an input string to a vector, I typically use
    > > something like this

    >
    > > vector<string> v;
    > > ifstream in("file");
    > > copy(istream_iterator<string> (in), istream_iterator<string>(),
    > > back_inserter(v));

    >
    > > However, I also tried to define istream_iterator separately. I did

    >
    > > istream_iterator<string> it(in);

    >
    > > this works, but there's no way I can define something equivalent to
    > > istream_iterator<string>() above. I tried

    >
    > > istream_iterator<string> it;

    >
    > > and this failed.

    >
    > It should work (apart from the fact that you seem to have used the
    > variable name 'it' twice). What error did you get?


    Thanks, I tried it again, and this error disappears. :) By the way,
    it seems both "istream_iterator it();" and "istream_iterator it;"
    work, is there any difference?
    Jess
     
    Jess, Jun 17, 2007
    #3
  4. Jess wrote:

    > On Jun 17, 11:16 pm, John Harrison <>
    > wrote:
    >> Jess wrote:
    >> > Hello,

    >>
    >> > When I copy contents from an input string to a vector, I typically use
    >> > something like this

    >>
    >> > vector<string> v;
    >> > ifstream in("file");
    >> > copy(istream_iterator<string> (in), istream_iterator<string>(),
    >> > back_inserter(v));

    >>
    >> > However, I also tried to define istream_iterator separately. I did

    >>
    >> > istream_iterator<string> it(in);

    >>
    >> > this works, but there's no way I can define something equivalent to
    >> > istream_iterator<string>() above. I tried

    >>
    >> > istream_iterator<string> it;

    >>
    >> > and this failed.

    >>
    >> It should work (apart from the fact that you seem to have used the
    >> variable name 'it' twice). What error did you get?

    >
    > Thanks, I tried it again, and this error disappears. :) By the way,
    > it seems both "istream_iterator it();" and "istream_iterator it;"
    > work, is there any difference?


    istream_iterator it();

    declares it as a function returning an istream_iterator, whereas

    istream_iterator it;

    defines it as an istream_iterator object and calls its default constructor.

    --
    rbh
     
    Robert Bauck Hamar, Jun 17, 2007
    #4
  5. Jess wrote:
    > On Jun 17, 11:16 pm, John Harrison <>
    > wrote:
    >> Jess wrote:
    >>> Hello,
    >>> When I copy contents from an input string to a vector, I typically use
    >>> something like this
    >>> vector<string> v;
    >>> ifstream in("file");
    >>> copy(istream_iterator<string> (in), istream_iterator<string>(),
    >>> back_inserter(v));
    >>> However, I also tried to define istream_iterator separately. I did
    >>> istream_iterator<string> it(in);
    >>> this works, but there's no way I can define something equivalent to
    >>> istream_iterator<string>() above. I tried
    >>> istream_iterator<string> it;
    >>> and this failed.

    >> It should work (apart from the fact that you seem to have used the
    >> variable name 'it' twice). What error did you get?

    >
    > Thanks, I tried it again, and this error disappears. :) By the way,
    > it seems both "istream_iterator it();" and "istream_iterator it;"
    > work, is there any difference?
    > Jess
    >


    Well this time I will say that it should NOT work. This

    istream_iterator<string> it;

    declares a variable called 'it' of type istream_iterator<string>, no
    problem. But this

    istream_iterator<string> it();

    declares a FUNCTION called 'it' which takes zero arguments and returns a
    istream_iterator<string>. This is a common newbie mistake, by adding the
    brackets you've written a function prototype.

    I guess this was the source of your original confusion.

    john
     
    John Harrison, Jun 17, 2007
    #5
  6. On Sun, 17 Jun 2007 06:52:21 -0700, Jess wrote:

    > I see my problems now, thanks!
    >
    > On the other hand, in program
    >
    > copy(istream_iterator<string> (in), istream_iterator<string>(),
    > back_inserter(v));
    >
    > Does it convert "in" to an istream_iterator<string>? If so, what is
    > "istream_iterator<string>()"? Neither of them looks like a function
    > call or constructing an istream_iterator<string> object...
    >
    > Jess


    'istream_iterator<string>(in)' creates a temporary object based on 'in'
    'istream_iterator<string>()' creates a temporary object representing the
    end iterator.
    'back_inserter(v)' creates a temporary object

    Consider:

    class T {};

    T t = T();

    --
    Obnoxious User
     
    Obnoxious User, Jun 17, 2007
    #6
  7. Jess

    Jess Guest

    I see my problems now, thanks!

    On the other hand, in program

    copy(istream_iterator<string> (in), istream_iterator<string>(),
    back_inserter(v));

    Does it convert "in" to an istream_iterator<string>? If so, what is
    "istream_iterator<string>()"? Neither of them looks like a function
    call or constructing an istream_iterator<string> object...

    Jess
     
    Jess, Jun 17, 2007
    #7
  8. Jess

    Jess Guest

    On Jun 17, 11:44 pm, Obnoxious User <O...@127.0.0.1> wrote:
    > On Sun, 17 Jun 2007 06:52:21 -0700, Jess wrote:
    > > I see my problems now, thanks!

    >
    > > On the other hand, in program

    >
    > > copy(istream_iterator<string> (in), istream_iterator<string>(),
    > > back_inserter(v));

    >
    > > Does it convert "in" to an istream_iterator<string>? If so, what is
    > > "istream_iterator<string>()"? Neither of them looks like a function
    > > call or constructing an istream_iterator<string> object...

    >
    > > Jess

    >
    > 'istream_iterator<string>(in)' creates a temporary object based on 'in'
    > 'istream_iterator<string>()' creates a temporary object representing the
    > end iterator.
    > 'back_inserter(v)' creates a temporary object
    > Consider:
    >
    > class T {};
    >
    > T t = T();


    I'm sometimes confused by the object creation syntax. To create an
    object t of type T, I think I can use the following statements.

    T t; //default constructor
    T t(arg); //constructor with argument arg
    T t = T(); //call default constructor for T()? then call copy
    constructor to create t using the temporary object created from T()
    T t = T(arg); //does the right hand side call the constructor with
    argument arg, then call the copy constructor to get t?
    T t = existing_t_object;
    T* tp = new T; //call default constructor
    T* tp = new T(arg); //constructor with argument arg

    Are they correct? Have I missed something?

    Thanks,
    Jess
     
    Jess, Jun 17, 2007
    #8
  9. Jess

    James Kanze Guest

    On Jun 17, 3:52 pm, Jess <> wrote:
    > I see my problems now, thanks!


    > On the other hand, in program


    > copy(istream_iterator<string> (in), istream_iterator<string>(),
    > back_inserter(v));


    > Does it convert "in" to an istream_iterator<string>? If so, what is
    > "istream_iterator<string>()"? Neither of them looks like a function
    > call or constructing an istream_iterator<string> object...


    According to the standard, they are both function style casts.
    The first converting in into an istream_iterator<string>, and
    the second converting nothing into an istream_iterator<string>.
    In practice, most people view them as explicit creation of a
    temporary, the first initialized with "in", and the second
    initialized using the default constructor. (Converting nothing
    into something doesn't sound much like a cast in most people's
    mind, regardless of what the standard calls it.)

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 17, 2007
    #9
  10. Jess

    James Kanze Guest

    On Jun 17, 4:25 pm, Jess <> wrote:
    > On Jun 17, 11:44 pm, Obnoxious User <O...@127.0.0.1> wrote:
    > > On Sun, 17 Jun 2007 06:52:21 -0700, Jess wrote:
    > > > I see my problems now, thanks!


    > > > On the other hand, in program


    > > > copy(istream_iterator<string> (in), istream_iterator<string>(),
    > > > back_inserter(v));


    > > > Does it convert "in" to an istream_iterator<string>? If so, what is
    > > > "istream_iterator<string>()"? Neither of them looks like a function
    > > > call or constructing an istream_iterator<string> object...


    > > 'istream_iterator<string>(in)' creates a temporary object based on 'in'
    > > 'istream_iterator<string>()' creates a temporary object representing the
    > > end iterator.
    > > 'back_inserter(v)' creates a temporary object
    > > Consider:


    > > class T {};


    > > T t = T();


    > I'm sometimes confused by the object creation syntax. To create an
    > object t of type T, I think I can use the following statements.


    > T t; //default constructor
    > T t(arg); //constructor with argument arg
    > T t = T(); //call default constructor for T()? then call copy
    > constructor to create t using the temporary object created from T()
    > T t = T(arg); //does the right hand side call the constructor with
    > argument arg, then call the copy constructor to get t?
    > T t = existing_t_object;
    > T* tp = new T; //call default constructor
    > T* tp = new T(arg); //constructor with argument arg


    > Are they correct? Have I missed something?


    They're all correct for creating named objects. There are,
    however, two types unnamed objects: those created using operator
    new (dynamically allocated objects), and temporary objects. A
    temporary object is the result of an expression: in the case of
    an object of class type, a function call or a "cast". Thus, for
    example: "static_cast< T >( arg )" behaves exactly like "T
    t(arg)", except that the resulting object is unnamed. Of
    course, there are two other ways of writing
    "static_cast<T>(arg)": "(T)arg" and "T(arg)". In the latter
    case, the standard also allows 0 or more than one argument; it
    still calls it a cast, even if it doesn't seem very logical.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 17, 2007
    #10
  11. Jess

    Jess Guest

    On Jun 18, 5:51 am, James Kanze <> wrote:
    > On Jun 17, 4:25 pm, Jess <> wrote:
    >
    >
    >
    > > On Jun 17, 11:44 pm, Obnoxious User <O...@127.0.0.1> wrote:
    > > > On Sun, 17 Jun 2007 06:52:21 -0700, Jess wrote:
    > > > > I see my problems now, thanks!
    > > > > On the other hand, in program
    > > > > copy(istream_iterator<string> (in), istream_iterator<string>(),
    > > > > back_inserter(v));
    > > > > Does it convert "in" to an istream_iterator<string>? If so, what is
    > > > > "istream_iterator<string>()"? Neither of them looks like a function
    > > > > call or constructing an istream_iterator<string> object...
    > > > 'istream_iterator<string>(in)' creates a temporary object based on 'in'
    > > > 'istream_iterator<string>()' creates a temporary object representing the
    > > > end iterator.
    > > > 'back_inserter(v)' creates a temporary object
    > > > Consider:
    > > > class T {};
    > > > T t = T();

    > > I'm sometimes confused by the object creation syntax. To create an
    > > object t of type T, I think I can use the following statements.
    > > T t; //default constructor
    > > T t(arg); //constructor with argument arg
    > > T t = T(); //call default constructor for T()? then call copy
    > > constructor to create t using the temporary object created from T()
    > > T t = T(arg); //does the right hand side call the constructor with
    > > argument arg, then call the copy constructor to get t?
    > > T t = existing_t_object;
    > > T* tp = new T; //call default constructor
    > > T* tp = new T(arg); //constructor with argument arg
    > > Are they correct? Have I missed something?

    >
    > They're all correct for creating named objects. There are,
    > however, two types unnamed objects: those created using operator
    > new (dynamically allocated objects), and temporary objects. A
    > temporary object is the result of an expression: in the case of
    > an object of class type, a function call or a "cast". Thus, for
    > example: "static_cast< T >( arg )" behaves exactly like "T
    > t(arg)", except that the resulting object is unnamed. Of
    > course, there are two other ways of writing
    > "static_cast<T>(arg)": "(T)arg" and "T(arg)". In the latter
    > case, the standard also allows 0 or more than one argument; it
    > still calls it a cast, even if it doesn't seem very logical.
    >
    > --
    > James Kanze (Gabi Software) email:
    > Conseils en informatique orientée objet/
    > Beratung in objektorientierter Datenverarbeitung
    > 9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


    I see now, thanks a lot!
    Jess
     
    Jess, Jun 18, 2007
    #11
    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. Replies:
    6
    Views:
    455
    CBFalconer
    Feb 24, 2007
  2. Replies:
    5
    Views:
    402
  3. Seguros Catatumbo

    Sending several input tags with the same name

    Seguros Catatumbo, Sep 5, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    354
    Mark Rae [MVP]
    Sep 5, 2007
  4. Replies:
    34
    Views:
    1,198
    James Kuyper
    May 29, 2009
  5. Victor \Zverok\ Shepelev

    RDOC: several related modules in several C files

    Victor \Zverok\ Shepelev, Mar 6, 2007, in forum: Ruby
    Replies:
    3
    Views:
    191
    Max Lapshin
    Mar 16, 2007
Loading...

Share This Page