繁体   English   中英

二维数组的数组

[英]Array of 2d arrays

我的问题很难描述,我有两个分别包含大量数字的表格; 对于一张表,我通过索引搜索格式

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)|
        +----+-----------+---------+---------+---------+

希望以下代码片段可以使我理解,

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
};

我已经包含资源多个2D阵列通过分组format ,每个具有用于不同尺寸不同format ,通过划index ,由COLED configure type等0,1,2..6,所以想要把它们放进另一个一维数组,以通过索引轻松查找,并最终获得资源,但是我不知道如何。

我尝试了以下操作,但失败了:

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

然后使用group[0] ,我可以获得format0 ,但是我发现它忘记了我需要知道的[1][2] ,所以我想知道有什么解决方案可以帮助我吗?

假设您在两个数组d1 (用于行数)和d2 (用于列数)中具有每个数组维度,则可以执行以下操作:

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

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

因此,您可以保持数组的尺寸接近,并可以像这样使用它们:

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!
        }
    }
}

否则,如果您在d1d2dimension没有dimension ,则在定义数组之后就无法知道它们,因此您将无法进行搜索(就像您说的那样)。

您的结构似乎没有包含太多实际数组/矩阵的方式。

我会用这样的东西:

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

然后定义一个函数来初始化实例:

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;
}

那么您可以轻松地构建一个数组:

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

错误检查将被忽略,但是在处理动态内存时必须考虑。

通过将组视为指针数组,您将失去将指针作为2D数组访问的能力。

一种选择是仅使用一维数组:

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}

当然,这不太方便初始化。

如果您实际上使用的是C ++,而不是C,则可以使用以std::vector和C ++ 11初始化程序列表为中心的以C ++为中心的解决方案:

#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';
}

但这仍然是不寻常的,我不确定您到底要解决什么问题。 如果您需要的是正确的Matrix类,则应编写一个此类,这样您就可以停止处理原始数组,而专注于您真正想要做的事情。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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