【问题标题】:Array of 2d arrays二维数组的数组
【发布时间】:2012-02-18 17:06:27
【问题描述】:

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

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

希望下面的代码sn-p能让我理解,

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

我有多个二维数组,其中包含按format 分组的资源,每个数组对于不同的format 有不同的维度,按index 排列,按configure type 分类,如0、1、2..6,所以我想要将它们放入另一个一维数组中,以便通过索引轻松查找,并最终获取资源,但我不知道如何。

我尝试了以下方法但失败了:

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

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

【问题讨论】:

  • 你的format0初始化错误,my_struct format0[1][2] = {{0, 1}};
  • 每个数组的维度如何?我的意思是你从哪里得到它们?
  • 嗨 TAMER,我有一张桌子,里面有很多数字,它们之间有内在的关系,尺寸是从那张桌子上得到的

标签: c++ c arrays


【解决方案1】:

假设您在两个数组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 中的维度,则在定义数组后无法知道它们,因此您将无法搜索(如你说你想做)。

【讨论】:

  • 嗨,驯兽师,感谢您的回复;我还想定义另一个结构来容纳我需要的所有东西,你的回答证实了我的想法。
  • 你不能像那样访问 arr[j][k] ,它只是一个指针。您将需要一个函数来计算 arr 的线性索引。
  • @Jonathan:是的,我可以,数组实际上是指针,您始终可以将[ ] 运算符与指针一起使用。
  • 是的,并且由于您将其编辑为 my_struct **,现在您可以访问 arr[][]> 但是,现在初始化不起作用,因为它将 format0 视为一个数组指针,事实并非如此。
  • 没错,但我假设format0, format1, etc.. 也是指针,因为他应该读取维度并因此动态分配这些数组,否则程序似乎没用。
【解决方案2】:

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

我会使用这样的东西:

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

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

【讨论】:

    【解决方案3】:

    将 group 视为指针数组,您将无法将指针视为 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}
    

    当然,这样初始化起来就不太方便了。

    【讨论】:

    • 对矩阵使用结构而不是简单数组的其他建议之一当然更安全和首选。
    • 谢谢,您的意思是当我将它们作为元素放入另一个数组时,数组会衰减为指针并丢失其尺寸,对吧?
    【解决方案4】:

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

    #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 类,那么您应该编写一个这样您就可以停止处理原始数组并专注于您真正想做的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多