views:

91

answers:

4

Hello everyone,

I need to create a sort of 2D array in which each one of the secondary arrays are of different length. I have a 1D array of known length (which defines the number of arrays to be formed) with each element having a number that denotes the length of the secondary array in that position.

Each one of the arrays are fairly large so i don't want to create a one-size-fits-all "fake" 2D heap array to cover everything.

How would i go about doing this? Any 2D array I have made before are always rectangular.

I'm trying to do this so that i can create some code to dynamically generate threads to split up some workload.

Thanks,

-Faken

+2  A: 

Use a std::vector of std::vectors - for integers:

#include <vector>
std::vector <std::vector <int> > v2d;
anon
I've never really understood the true point of vectors. Other than having automatic memory management and being slightly easier for the programmer to deal with, dose it offer any real performance advantages if all I'm doing is iterating over the set of data without ever having to insert data into it?
Faken
Being easy to use is a pretty big win for most programmers! They are no more efficient than arrays, but no less so either, if compiled with full optimisations.
anon
A: 

Some sort of sparse array might be helpful here. There's this question at stack overflow or this site.

Good luck.

Bob Jarvis
A: 

Or if you're thinking more along the lines of C (there are zillions of people writing C code but compiling it with C++, that mostly works):

Declare not a two dimensional array: int arr[rows][cols]

but a one dimensional array of pointers to arrays: int (arr*)[rows]

Not quite sure about that syntax, someone correct me if I'm wrong please!

Carl Smotricz
Actually that is exactly what I'm trying to do. I need to initialize an array of pointers but i don't know the syntax for it.
Faken
Looks like Rahul G agrees and did a nicer job with the examples.
Carl Smotricz
+3  A: 

You can do something like following :

int **array = new int*[3];
array[0] = new int[5];
array[1] = new int[2];
array[2] = new int[11];

Do not forget to deallocate properly after you are done with using array.

EDIT :

You can put secondary pointer initialization in the loop in the following way :

int cells[] = {5,2,11};
int **array = new int*[3];
for (int i = 0; i < 3; i++){
  array[i] = new int[cells[i]];
}

PS : I personally prefer to use Boost.MultiArray when I need multidimensional arrays. You might also want to have a look at it.

missingfaktor
Ahh thank you, this is ALMOST it, but i just need to put the secondary pointer initialization into a loop...ill let you know how it goes.
Faken
Tell us how you know how big to make the secondary arrays and we'll tell you how to code it :)
Carl Smotricz
Yup, thats it. Just needed to put it in a loop and its done. Why wasn't I able to figure this out myself?...maybe it has something to do with the fact thats its 6:20 am and I'm tired yet still can't sleep...
Faken
@Carl: The size of the secondary arrays is related to the total number of valid clamping locations...which...brings me to my thesis in mechanical engineering...which has very little to do with mechanical engineering yet can not be explained without nearly 4 years of background from studying mechanical engineering...
Faken
**@Whoever downvoted :** Would you please care to comment?
missingfaktor