Arrays of NRVec
maugis
09-16-2003, 10:52 AM
Hello.
I am new to C++ and I didn't find the way to define an array of vectors, and incorporate it as a member of a class. The Visual C++ compiler does not allow for it. Is there a solution?
Thanks in advance.
Philippe.
Kevin Dolan
09-24-2003, 05:09 PM
Should be no problem.
Static array:
Vec_DP x[10];
Dynamic array:
Vec_DP *x = new Vec_DP [10];
...
delete [] x;
In both cases, the default constructor will be used to initialize the vectors, so they will have zero size. This means that you will have to use the assignment operator to make them the size you want. For example:
Vec_DP dummy(100);
for (i = 0; i < 10; ++i) x[i] = dummy;
Do you really need an array of them, though? A far better solution would be to just make a vector of vectors, like this:
NRVec<Vec_DP> x(10);
This will also result in each Vec_DP having zero size. To make them all have a length of 100:
NRVec<Vec_DP> x(Vec_DP(100), 10);
This passes a temporary Vec_DP of size 100 to the constructor, which will result in each Vec_DP having a size of 100.
To make such an array a member of a class, you would need to do something like this:
class foo {
NRVec<Vec_DP> x;
public:
foo(int n, int m) : x(Vec_DP(m), n) {;}
...
};
Kevin
[edited to correct a typo.]
maugis
09-25-2003, 05:46 AM
Thank you Kevin.
What seamed to me impossible at first has become very simple to do thanks to you!
Of course, in your answer, the first construction scheme has to be understood as:
Vec_DP dummy(100);
for (int i = 0; i < 10; ++i) x[i] = dummy;
instead of the same with 'temp'?
There remains a serious restriction to the use of NRVec in practice with Visual C++: it is the fact that the elements of the vectors do not appear in the 'watch' window of the debugger. Only the first element of the vector is visible. This does not apply to arrays.
Philippe.
Kevin Dolan
09-25-2003, 08:47 AM
Yeah, the "temp" should be "dummy". I fixed it above.
I don't think there is much to be done about the debugger issue. That will be a problem for any scheme which involves dynamically allocated arrays.
Kevin
gudum35
07-21-2004, 11:01 AM
Is it possible to make a matrix of matrices using something like:
NRMat<Mat_DP> x(2,2);
NRMat<Mat_DP> x(Mat_DP(10,10),2,2);
and
class foo {
NRMat<Mat_DP> x;
public:
foo(int n, int m, int i, int j) : x(Mat_DP(i,j), n, m) {;}
...
};
?
Thanks
Guillaume
maugis
07-22-2004, 05:04 AM
Thank you for your help.
Philippe.