Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > constructing vector<POD> that is member of a class from an input stream (file)

Reply
Thread Tools

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

 
 
Hicham Mouline
Guest
Posts: n/a
 
      11-17-2009
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,

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      11-17-2009
On Nov 17, 12:02*pm, "Hicham Mouline" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      11-18-2009
On Nov 17, 10:20 pm, red floyd <(E-Mail Removed)> wrote:
> On Nov 17, 12:02 pm, "Hicham Mouline" <(E-Mail Removed)> 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
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      11-18-2009
On Nov 18, 12:53*am, James Kanze <(E-Mail Removed)> wrote:
> On Nov 17, 10:20 pm, red floyd <(E-Mail Removed)> wrote:
>
>
>
> > On Nov 17, 12:02 pm, "Hicham Mouline" <(E-Mail Removed)> 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").


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Constructing a URI that uses a stream as input Qu0ll Java 0 07-04-2009 03:40 PM
Constructing MIME message without loading message stream Kris Kennaway Python 2 08-10-2008 11:21 PM
constructing objects from a stream jesper@alphacash.se C++ 1 02-23-2006 08:40 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM



Advertisments