C++: Recently, I have come to need (or at least think I do) to put a resizable multidimensional array into a class. Now I know that using vector arrays isn't a good way to do this... what would be a good way to do this?
C++: Recently, I have come to need (or at least think I do) to put a resizable multidimensional array into a class. Now I know that using vector arrays isn't a good way to do this... what would be a good way to do this?
First step is to know what you need.
I would always suggest using STL vectors instead of C style arrays, so I don't know what you mean here. For an example:Now I know that using vector arrays isn't a good way to do this... what would be a good way to do this?
PHP Code:
template <typename T>
class Matrix{
private:
std::vector<std::vector<T> > multiArray;
};
When I invented the Web, I didn't have to ask anyone's permission.
~Tim Berners-Lee on Net Neutrality
-------------------------------------
Visit the Ubuntu Programming IRC-channel at #ubuntu-programming (chat.freenode.net).
I've searched the subject rather thoroughly trying to solve my problem myself, and I'm pretty sure your example turns out like crap. Each array contained in an array (in your example) would have to be resized to the proper size to have a standard sized second dimension, would they not? And I need to work with up to 3 or 4 dimensions, so this approach simply would not do.I would always suggest using STL vectors instead of C style arrays, so I don't know what you mean here. For an example:
But that is... as I said... to my knowledge.
Perhaps something like this:
Does this help? With this model, its easy to make a 5 dimensional array where each branch is a different length. You can also generalizes this to more dimensions with int*** array or int**** array:Code://create 5x5 array int** array = new int*[5]; int row1[5] = {1,2,3,4,5}; int row2[5] = {6,7,8,9,10}; ... array[0]= &row1[0]; array[1] = &row2[0]; cout << array[0][1]; //2 cout << array[1][2]; //8 //change to 6x6 array; int** temp = array; delete[] array; array = new int*[6]; ...//copy data
Code:int*** array = new int**[5]; int** branch1 = new int*[5]; int branch1row1[5] = {1,2,3,4,5}; branch1[0] = &branch1row1[0]; array[0] = &branch1[0]; cout << array[0][0][2]; //3
Last edited by hundredwatt; January 7th, 2009 at 11:26 PM. Reason: Debugging...
How about a hashtable that takes some two-value X,Y object as keys? In this case, you could expand it indefinitely, and you can just assume some default value for all the keys that aren't set.
What are you doing that you think you need 3-4 dimensions for? If you let us know what it is that you're aiming for we can likely be of more help to you.
And the thing with vectors is that they resize themselves, you don't have to manage it. If you need another item, push it on.
The easiest solution when dealing with multidimensional arrays is to use the array type as defined in the boost libraries.
Here's an example:
PHP Code:
#include <boost/array.hpp>
#include <iostream>
int main()
{
typedef boost::array<int, 3> OneD;
typedef boost::array<OneD, 3> TwoD;
typedef boost::array<TwoD, 3> ThreeD;
typedef boost::array<ThreeD, 3> FourD;
FourD array4D;
array4D[0][0][0][0] = 10;
array4D[1][1][1][1] = 20;
array4D[2][2][2][2] = 30;
std::cout << "array4D[0][0][0][0] = " << array4D[0][0][0][0] << std::endl;
std::cout << "array4D[1][1][1][1] = " << array4D[1][1][1][1] << std::endl;
std::cout << "array4D[2][2][2][2] = " << array4D[2][2][2][2] << std::endl;
return 0;
}
I'm sorry I'm so blunt.Ouch; tough remark to make to someone considering that person was trying to help you.
But am I not correct in that, using so may vector arrays just to get a 3 dimensional array would incur vast amounts of waste? We're talking all the extra data required by a vector, multiplied by what?
As far as the 3 dimensions goes. I'm storing voxel information... I think I've taken the instance where I needed 4 dimensions out of my code.What are you doing that you think you need 3-4 dimensions for?
I'm not familiar with hashtables.How about a hashtable that takes some two-value X,Y object as keys?
I'll toy around with that, but wouldn't "int** temp = array;" merely point to what's contained by array, and then, of course, it would be deleted in the next line?Perhaps something like this:
Array handles multidimensional arrays? I thought only Multi_Array did!.. and I couldn't get that to work in a structure... this will work?The easiest solution when dealing with multidimensional arrays is to use the array type as defined in the boost libraries.
...Oh wait... I see. Wouldn't that be almost as bad as vector?
Last edited by Zeotronic; January 8th, 2009 at 02:07 AM.
You can use std::map as the hashtable, and std:air for the keys of a 2-dimensional table.
For larger dimensions you can just make your own key class or use something like boosts tuple.Code:#include <map> #include <iostream> typedef std::pair<int,int> vec2; int main() { std::map<vec2, int> grid; grid[vec2(10, 10)] = 10; grid[vec2(10000, 10000)] = 7; std::cout << grid[vec2(10, 10)] << std::endl; std::cout << grid[vec2(10000, 10000)] << std::endl; return 0; }
Bookmarks