Michael Hopkins wrote:
[color=blue]
> I have a class that contains as one of it's members a type
> that is internally constructed like this:[/color]
[color=blue]
> std::vector< std::valarray< float > > foo[/color]
[color=blue]
> Think of it as a matrix. I cannot construct foo when the
> class itself is constructed because I don't know how big it
> needs to be until I have loaded that information from a file.[/color]
No problem. If you use the default constructor, the vector is
empty.
[color=blue]
> Within the member function that loads the file (including the
> information r & c), this compiles OK:[/color]
[color=blue]
> std::vector< std::valarray< float > > temp( r, std::valarray<float>([/color]
c ));[color=blue]
> foo = temp;[/color]
[color=blue]
> ...but I'm sure there is a more elegant/efficient way to do
> this on foo directly - any tips on this?[/color]
I don't have any problem with doing it this way, although it can
be relatively inefficient if the array is large.
[color=blue]
> I have defined access to this type like a Fortran array
> i.e. foo( i, j ) either reads from or writes to the j'th
> valarray element of the i'th vector. This works fine in
> general usage but, not when I am trying to load from the input
> file in a loop:[/color]
[color=blue]
> std::ifstream infile( from_path );
> // in i, j, loop
> infile >> foo( i, j );[/color]
[color=blue]
> Again, any tips welcomed.[/color]
I'm not sure how you could define access to the foo object like
this -- you can't add functions to std::vector, and the
operator()() must be a member.
Anyway, with regards to your problem, the obvious solution would
be to use push_back on the vector for each line of data you
read. With regards to the valarray part, I have relatively
little experience; I think you'll have to experiment to find out
whether it is faster to use a local valarray, then push_back it,
or to push_back an empty (default constructed) valarray, then
use resize on the element in the vector.
I might add that if you need a Fortran compatible array, this
will not do the job. For that, you'll probable have to use a
simple std::vector< float >, and manage the dimensions yourself.
--
James Kanze GABI Software
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
[ See
http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]