Click to See Complete Forum and Search --> : Dynamic Multidimensional Array
mbanghart
August 8th, 2008, 07:22 AM
Hi,
How do I create a multidimensional array[r][c] with a dynamic size? My input file contains the r and c values, but can be different for each input file.
I know that you can specify a dynamic array within a function (such as my readfile function, but how do I pass that array to another function?
Thanks
Lindley
August 8th, 2008, 07:37 AM
There are several ways to do it; the easiest is to just use a vector< vector< int > > as your container. This is recommended if you want to be able to resize both dimensions independently.
If you will only ever resize both dimensions at once, there's a slightly more efficient way to do it using two variables, a vector<int> and a vector<int*>. I can elaborate on that if you like.
Of course, it's also possible to do this using a plain old array.
mbanghart
August 8th, 2008, 07:42 AM
I m not familiar with the vector container at all. I found another way to do it - to basically pass the array from function to function.
Do you think that would cause significant processing time? My data will become large, prob 800 x 800 arrays.
Paul McKenzie
August 8th, 2008, 07:48 AM
Hi,
How do I create a multidimensional array[r][c] with a dynamic size?Did you do a search on CodeGuru? There are at least 50 to 100 threads that show how to do this, with complete examples.
Regards,
Paul McKenzie
mbanghart
August 8th, 2008, 07:58 AM
Actually think vector might be what I need.
How do I populate the array since its two dimensional?
I will look around on codeguru.
Hermit
August 8th, 2008, 08:32 AM
Did you do a search on CodeGuru? There are at least 50 to 100 threads that show how to do this, with complete examples.
Regards,
Paul McKenzie
To be fair, the search feature of this forum (and most forums) is useless, which is why in the past I've shyed away from assuming that someone did not try to search the forums before posting. Fortunately, a google search containing the keyword "codeguru" or "site:www.codeguru.com" can be very effective, so I'd recommend that instead.
mbanghart
August 8th, 2008, 08:43 AM
I managed to get it working. How can I delete an entire row from a multidimensional vector:
Here is how I initialize inputMatrix:
vector<int> v(columns);
vector<vector <int> > inputMatrix(rows,v);
Other assumptions: Rows and columns is set somewhere else. inputMatrix is populated correctly when the following code executes:
Here is my code:
//Start from top
for (r = 0; r < rows; r++)
{
for (c = 0; c < columns; c++)
{
if (inputMatrix[r][c] == 1)
oneCounter++;
}//inner for c
if (oneCounter < pruneThreshold)
{
if (stopFlag == true)
break;
else
{
//delete entire row r
//UNSURE
}//else
}//if
else
{
//set flag to ensure data integrity
stopFlag = true;
}//else
}//outer for
Lindley
August 8th, 2008, 09:04 AM
I found another way to do it - to basically pass the array from function to function.
Do you think that would cause significant processing time? My data will become large, prob 800 x 800 arrays.
There's a subtlety here you need to be aware of. When you're passing around a plain old array, you aren't copying the *array* from function to function, just the pointer to it. Therefore, it's very cheap to pass it to a function.
But when you're using a std::vector, then the entire array *will* be copied if you let it-----and that's very expensive, so it's best to avoid it. For this reason, std::vectors (and all STL containers, actually) should always be passed by reference, not by value. Pass by const reference if you don't want to change it, but still by reference. The only time you should pass by value is if you would otherwise make a duplicate of the container inside the function anyway.
How can I delete an entire row from a multidimensional vector:
All you have to do is delete one of the vectors inside the outer vector. This could be done by:
inputMatrix.erase(inputMatrix.begin()+r);
However, this is not something you should do often, because (as always with array-like things), it requires all subsequent elements to be copied backwards one position. Since you're talking about copying entire vectors, that can be costly.
There is an optimization you could do if you need speed; maybe the STL implementation would even do it itself, although I doubt it. That would be to essentially "bubble" the offending row to the bottom of the matrix using the vector::swap function, and then just call inputMatrix.pop_back(). However, as always, don't try such optimizations until you've determined that the "clean" way of writing the code is too slow.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.