0

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

Faken
  • 11,352
  • 17
  • 59
  • 74

4 Answers4

2

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

#include <vector>
std::vector <std::vector <int> > v2d;
  • 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 Jan 05 '10 at 12:38
  • 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. –  Jan 05 '10 at 14:37
2

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
  • 90,905
  • 62
  • 285
  • 365
  • 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 Jan 05 '10 at 12:17
  • 1
    Tell us how you know how big to make the secondary arrays and we'll tell you how to code it :) – Carl Smotricz Jan 05 '10 at 12:21
  • 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 Jan 05 '10 at 12:21
  • @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 Jan 05 '10 at 12:24
0

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

Good luck.

Community
  • 1
  • 1
0

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
  • 66,391
  • 18
  • 125
  • 167