简体   繁体   中英

Array of 2d arrays

My question is difficult to describe, and I have two tables containing lots of numbers respectively; for one table, I search format through index

table1   index format  
        +------+----+                        
        |0~19  |  0 |
        |      |    |
        +------+----+
        |20~29 |  1 |
        |      |    |
        +------+----+
        |30~39 |  2 |
        |      |    |
        +------+----+

table2  index  resource(f,t0,t1,t2)  
                  0           1        2         3 (configure type)
        +----+-----------+---------+---------+                      
        |0   | (0,1,0,2) |(0,1,0,1)|(0,1,0,0)|
        +----+-----------+---------+---------+
        |1   | (0,2,0,2) |(0,2,0,1)|(0,2,0,0)|
        +----+-----------+---------+---------+
        |--  | (0,0,1,2) |(0,0,1,1)|(1,0,0,0)|
        +----+-----------+---------+---------+
        |19  | (0,0,0,0) |(0,0,0,0)|(0,0,1,1)|
        +----+-----------+---------+---------+---------+
        |--  | (0,0,0,2) |(0,0,0,1)|(0,0,1,0)|(0,2,1,0)|
        +----+-----------+---------+---------+---------+
        |29  | (0,1,0,2) |(0,1,0,1)|(0,1,0,1)|(0,1,0,1)|
        +----+-----------+---------+---------+---------+

Hopefully,the following code snippet can make me understood,

typedef struct my_struct {
    int f;
    int t0;
    int t1;
    int t2;
} my_struct;

// for index 0 ~ 19, the following is code snippet
    my_struct format0[2][3] = {
        {{0, 1, 0, 2}, {0, 1, 0, 1},{0, 1, 0, 0}}, // index 0
        {{0, 2, 0, 2}, {0, 2, 0, 1},{0, 2, 0, 0}}  // index 1
    };

// for index 20 ~ 29, the following is code snippet    
my_struct format1[1][4] = {
    {{0,0,0,2},{0,0,0,1},{0,0,1,0},{0,2,1,0}} // index 20
};

I have multiple 2d arrays containing resources grouped by format , each has different dimensions for different format , rowed by index , coled by configure type like 0,1,2..6, so I want to put them into another 1d array in order to look up easily through index, and get resource finally, but I don't know how.

I have tried the following but failed:

my_struct* group[] = {
    format0,
    format1
};

then using group[0] , I can get format0 , but I find it forgot its [1][2] which I need to know, so I am wondering there is some solution help me do that?

Assuming that you have each the arrays dimensions in two arrays d1 (for the number of rows) and d2 (for the number of columns), you can do this:

struct foo {
    my_struct** arr;
    int dim1;
            int dim2;
};

foo group[dimension] = {
    {format0,d1[0],d2[0]},
    {format1,d1[1],d2[1]},
};

Thus you keep the array's dimension close, and can use them like this for example:

for (int i = 0;i<dimension;i++)
{
    for(int j = 0;j<group[i].dim1;j++)
    {
        for (int k=0;k<group[i].dim2;k++)
        {
            group[i].arr[j][k]; // do something with it!
        }
    }
}

Otherwise if you don't have the dimensions in d1 , d2 and dimension , it's not possible to know them later after the arrays are define, and thus you won't be able to search (as you say you want to do).

Your struct doesn't seem to contain much in the way of actual arrays/matrices.

I'd use something like this:

typedef struct
{
  size_t width, height;
  int    *data;
} Matrix2D;

then define a function to initialize an instance:

int matrix2d_new(Matrix2D *out, size_t width, size_t height)
{
  if(out == NULL || width == 0 || height == 0)
    return 0;
  if((out->data = malloc(width * height * sizeof *out->data)) != NULL)
  {
    out->width = width;
    out->height = height;
    memset(out->data, 0, width * height * sizeof *out->data);
  }
  return out != NULL;
}

then you can build an array trivially:

Matrix2D matrices[3];
matrix2d_new(&matrices[0], 12, 34);
matrix2d_new(&matrices[1], 40, 50);
matrix2d_new(&matrices[2], 20, 50);

Error-checking is ignored, but must of course be considered when dealing with dynamic memory.

By treating group as an array of pointers you lose the ability to access the pointers as 2D arrays.

One option is to use only 1D arrays:

my_struct format00[3] = {{1,2,3}, {4,5,6}, {7,8,9}};
my_struct format01[3] = {{10,20,30}, {40,50,60}, {70,80,90}};

my_struct *format0[2] = {format00, format01};
my_struct *format1[2] = {format10, format11};

my_struct **group[] = {format0, format1};

my_struct a = group[0][0][1];  // {4,5,6}

Of course, this is much less convenient to initialize.

If you're actually using C++ and not C, then you can use a more C++-centric solution, with std::vector and C++11 initialiser lists:

#include <iostream>
#include <vector>

using std::cout;
using std::vector;

typedef struct my_struct {
    int i;
    int j;
    int k;
} my_struct;

typedef vector<vector<my_struct>> Matrix;

Matrix format0 {
    {{0, 1, 2}, {1, 2, 3}}
};

Matrix format1 {
    {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}},
    {{3, 4, 5}, {4, 5, 6}, {5, 6, 7}}
};

vector<Matrix*> groups { &format0, &format1 };

int main(int argc, char** argv) {
    for (auto i = groups.begin(); i != groups.end(); ++i)
        cout << (**i).size() << 'x' << (**i)[0].size() << '\n';
}

But this is still unusual, and I'm not sure what problem you're really trying to solve. If what you need is a proper Matrix class, then you should write one so you can stop dealing with raw arrays and focus on what you really want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM