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

H

Hicham Mouline

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,
 
R

red floyd

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.
 
J

James Kanze

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).
 
R

red floyd

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").
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top