Up to now, I have been creating dynamic arrays the old-fashioned way: using new and delete and manually taking care of memory/pointer house-keeping.
I would now like to get comfortable with the new way of handling dynamic arrays: using <vector>.
I have written a small program that passes a 2D array into a sub-routine, performs some manipulations on the array elements, and then returns the array to the main program. Here is a drastically abridged version of the program:
#include <vector>
. . .
void function_M(vector<vector<double> > input_2D_Array) {
// perform some operations on the elements of the 2D matrix
// for example, multiply all elements by 2
. . .
return;
}
. . .
int main()
vector<vector<double> > Matrix_A; // Create 2D matrix
int M, N; // The number of rows and columns of matrix
int i; // array index
. . .
// prompt user to enter matrix dimensions, M and N. Then re-size the matrix to
// the specified dimensions: M X N
. . .
Matrix_A.resize(M);
for (i = 0; i < M; i++) Matrix_A[i].resize(N);
. . .
// Pass Matrix_A into the sub-routine to perform the necessary operations
function_M(Matrix_A);
// Output Matrix_A to check results
. . .
return 0;
}
When I step through the program, the array is properly changed within the sub-routine. However, the changes don't seem to be passed out to the main program. I had assumed arrays were always passed by reference, so any changes made within the sub-routine on an array variable were changed in the main program too. Was I mistaken? If so, how do I correct it?
(Any other advice for making this a better program would be welcome too.)
I think you need to put an & and send the adress of the vector through. I am also very new to this vector thing as well though ...
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <vector>
. . .
void function_M(vector<vector<double> > & input_2D_Array) {
// perform some operations on the elements of the 2D matrix
// for example, multiply all elements by 2
. . .
return;
}
Thanks for the suggestion. That works! I didn't change anything else.
However, I am a little puzzled about why it works.
If the type of parameter accepted by the sub-routine changes, shouldn't the type of parameter fed into the sub-routine also have to change (which I did not do)?
Perhaps I am confusing it with the old C way of defining sub-routines with pointers (e.g. - int* intFlag) and also feeding in the address of the variable instead of the variable itself when calling the sub-routine (e.g. - &intFlag).
In this case, I added the ampersand (&) to the parameter list in the sub-routine definition, but I still fed in the variable itself when calling the sub-routine. Is this correct?
"toum": Anytime I look for information about dynamic arrays, <vectors> are the recommended approach. However, you say this approach is inefficient. What approach would you recommend for dynamic arrays of 2 dimensions (or higher)?
If the type of parameter accepted by the sub-routine changes, shouldn't the type of parameter fed into the sub-routine also have to change (which I did not do)?
The compiler is allowed to perform some type conversion automatically. For example you never get a compile error when you pass an int variable to a function that expects an unsigned int. The conversion of any type to its reference is one of these allowed automatic conversions.
"toum": Anytime I look for information about dynamic arrays, <vectors> are the recommended approach. However, you say this approach is inefficient. What approach would you recommend for dynamic arrays of 2 dimensions (or higher)?
I did not say vectors are not efficient, but that vectors of vectors are not.
It's the equivalent of doing that: