constructing vector<POD> that is member of a class from an input stream (file)

Discussion in 'C++' started by Hicham Mouline, Nov 17, 2009.

  1. Hello,

    I have a text file with this format:

    floating11 floating12 floating13 floating14
    floating21 floating22 floating23 floating24
    ....
    floatingN1 floatingN2 floatingN3 floatingN4


    I have a simple struct

    struct F {
    double d1;
    double d2;
    double d3;
    double d4;
    };

    and a
    std::vector<F> v;

    then a class C which adds intelligence to the processing of the vector of
    Fs.
    class C {
    C(const std::istream& input);
    private:
    std::vector<F> v_;
    };

    I wish to construct an instance of C from a file.
    I thought to use the iterator form of vector<F>'s ctor.

    Do I write an iterator class that when dereferenced, points to an instance
    of F, so that I can do

    C::C(const std::istream& input)
    : v_( begin, end )
    {}

    Was there a stream_iterator in std?

    I understand there is some elegant form to fill up a vector as;
    std::copy( ? , ? , v_.back_inserter() );

    Is there a similar form for vector construction?

    regards,
     
    Hicham Mouline, Nov 17, 2009
    #1
    1. Advertising

  2. Hicham Mouline

    red floyd Guest

    Re: constructing vector<POD> that is member of a class from an inputstream (file)

    On Nov 17, 12:02 pm, "Hicham Mouline" <> wrote:
    > Hello,
    >
    > I have a text file with this format:
    >
    > floating11 floating12 floating13 floating14
    > floating21 floating22 floating23 floating24
    > ...
    > floatingN1 floatingN2 floatingN3 floatingN4
    >
    > I have a simple struct
    >
    > struct F {
    >   double d1;
    >   double d2;
    >   double d3;
    >   double d4;
    >
    > };
    >
    > and a
    > std::vector<F> v;
    >
    > then a class C which adds intelligence to the processing of the vector of
    > Fs.
    > class C {
    >   C(const std::istream& input);
    > private:
    >   std::vector<F> v_;
    >
    > };
    >
    > I wish to construct an instance of C from a file.
    > I thought to use the iterator form of vector<F>'s ctor.
    >
    > Do I write an iterator class that when dereferenced, points to an instance
    > of F, so that I can do
    >
    > C::C(const std::istream& input)
    > : v_( begin, end )
    > {}
    >
    > Was there a stream_iterator in std?
    >
    > I understand there is some elegant form to fill up a vector as;
    > std::copy( ? , ? , v_.back_inserter() );
    >
    > Is there a similar form for vector construction?
    >
    >


    Use std::istream_iterator<>.
    You need to define operator<< for F.
     
    red floyd, Nov 17, 2009
    #2
    1. Advertising

  3. Hicham Mouline

    James Kanze Guest

    Re: constructing vector<POD> that is member of a class from an inputstream (file)

    On Nov 17, 10:20 pm, red floyd <> wrote:
    > On Nov 17, 12:02 pm, "Hicham Mouline" <> wrote:


    > > I have a text file with this format:


    > > floating11 floating12 floating13 floating14
    > > floating21 floating22 floating23 floating24
    > > ...
    > > floatingN1 floatingN2 floatingN3 floatingN4


    > > I have a simple struct


    > > struct F {
    > > double d1;
    > > double d2;
    > > double d3;
    > > double d4;
    > > };


    > > and a
    > > std::vector<F> v;


    > > then a class C which adds intelligence to the processing of
    > > the vector of Fs.
    > > class C {
    > > C(const std::istream& input);
    > > private:
    > > std::vector<F> v_;
    > > };


    > > I wish to construct an instance of C from a file.
    > > I thought to use the iterator form of vector<F>'s ctor.


    > > Do I write an iterator class that when dereferenced, points
    > > to an instance of F, so that I can do


    > > C::C(const std::istream& input)
    > > : v_( begin, end )
    > > {}


    > > Was there a stream_iterator in std?


    > > I understand there is some elegant form to fill up a vector
    > > as;
    > > std::copy( ? , ? , v_.back_inserter() );


    > > Is there a similar form for vector construction?


    > Use std::istream_iterator<>.
    > You need to define operator<< for F.


    That would be operator>>, of course, for input. And the two
    iterator constructor for vector (paying attention that you
    actually do define a vector, and not just declare a function
    that returns one---you'll typically need an extra pair of
    parentheses somewhere).

    --
    James Kanze
     
    James Kanze, Nov 18, 2009
    #3
  4. Hicham Mouline

    red floyd Guest

    Re: constructing vector<POD> that is member of a class from an inputstream (file)

    On Nov 18, 12:53 am, James Kanze <> wrote:
    > On Nov 17, 10:20 pm, red floyd <> wrote:
    >
    >
    >
    > > On Nov 17, 12:02 pm, "Hicham Mouline" <> wrote:
    > > > I have a text file with this format:
    > > > floating11 floating12 floating13 floating14
    > > > floating21 floating22 floating23 floating24
    > > > ...
    > > > floatingN1 floatingN2 floatingN3 floatingN4
    > > > I have a simple struct
    > > > struct F {
    > > >   double d1;
    > > >   double d2;
    > > >   double d3;
    > > >   double d4;
    > > > };
    > > > and a
    > > > std::vector<F> v;
    > > > then a class C which adds intelligence to the processing of
    > > > the vector of Fs.
    > > > class C {
    > > >   C(const std::istream& input);
    > > > private:
    > > >   std::vector<F> v_;
    > > > };
    > > > I wish to construct an instance of C from a file.
    > > > I thought to use the iterator form of vector<F>'s ctor.
    > > > Do I write an iterator class that when dereferenced, points
    > > > to an instance of F, so that I can do
    > > > C::C(const std::istream& input)
    > > > : v_( begin, end )
    > > > {}
    > > > Was there a stream_iterator in std?
    > > > I understand there is some elegant form to fill up a vector
    > > > as;
    > > > std::copy( ? , ? , v_.back_inserter() );
    > > > Is there a similar form for vector construction?

    > > Use std::istream_iterator<>.
    > > You need to define operator<< for F.

    >
    > That would be operator>>, of course, for input.  And the two
    > iterator constructor for vector (paying attention that you
    > actually do define a vector, and not just declare a function
    > that returns one---you'll typically need an extra pair of
    > parentheses somewhere).
    >


    Thanks, James. Silly typo, I meant >>.

    And James is also correct about the parens (google "most vexing
    parse").
     
    red floyd, Nov 18, 2009
    #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. ding feng
    Replies:
    8
    Views:
    854
    Dhruv
    Jul 2, 2003
  2. Ajax Chelsea

    Is array of POD still a POD type?

    Ajax Chelsea, Dec 1, 2003, in forum: C++
    Replies:
    1
    Views:
    348
    Ron Natalie
    Dec 1, 2003
  3. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  4. Qu0ll
    Replies:
    0
    Views:
    341
    Qu0ll
    Jul 4, 2009
  5. Himanshu Garg
    Replies:
    1
    Views:
    166
    Himanshu Garg
    Sep 21, 2003
Loading...

Share This Page