The way I solved this was to declare an empty array:
Array<double> A;
and then grow it as required as the data is read, using resizeAndPreserve. If you read from a file it is better to grow it in chunks, and at the end shrink to size, rather than growing element by element:
A.resize(500); // make room for 500 elements
i=0;
while(!file.eof()) // read to the end of the file
{
file >> A(i);
i++;
if(i>=A.size()) // make room for more elements if needed
A.resizeAndPreserve(i+500);
}
A.resizeAndPreserve(i); //shrink to the correct size when finished
I am typing by memory, so there are probably some mistakes, but you get the idea. You will have to play with the size of the chunks to get the optimal value for the application you have in hand.
Hope this helps.
Carlos A. Rega, PhD
Development Scientist
Malvern Instruments Ltd
Grovewood Rd, Malvern
WR14 1XZ
Tel: +44 (0)1684 581 304
Fax: +44 (0)1684 892 789
e-mail: carlos.rega@malvern.co.uk
Shmuel Brody <sbrody@cs.huji.ac.il> Sent by: blitz-support-bounces@oonumerics.org
16/07/2004 10:08
Please respond to Support list for Blitz++
To: blitz-support@oonumerics.org
cc:
Subject: [Blitz-support] arrays with dimension only known at runtime.
Is there any way to declare Arrays/TinyVectors when the number of
dimensions in only known at runtime? for instance, read from a file or
user input?
_______________________________________________
Blitz-support mailing list
Blitz-support@oonumerics.org
http://www.oonumerics.org/mailman/listinfo.cgi/blitz-support
-------------------------------------------------------------------------------------------------------------------- This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed.
If you have received this email in error please notify the originator of the message.
Any views expressed in this message are those of the individual sender, except where the sender specifies and with authority, states them to be the views of Malvern Instruments.