Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 二维阵列的阵列_C++_C_Arrays - Fatal编程技术网

C++ 二维阵列的阵列

C++ 二维阵列的阵列,c++,c,arrays,C++,C,Arrays,我的问题很难描述,我有两张分别包含大量数字的表格;对于一个表,我通过索引搜索格式 table1 index format +------+----+ |0~19 | 0 | | | | +------+----+ |20~29 | 1 | | | | +------+----+

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

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数组,包含按
格式
分组的资源,每个数组对不同的
格式
有不同的维度,按
索引
排列,按
配置类型
收集,如0,1,2..6,所以我想把它们放在另一个1d数组中,以便通过索引轻松查找,最终获得资源,但我不知道怎么做

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

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

for(int i=0;i假设每个数组都有两个数组
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!
        }
    }
}

for(inti=0;i您的结构似乎没有实际数组/矩阵的形式

我会用这样的方法:

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

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

您的结构似乎没有实际数组/矩阵那样包含太多内容

我会用这样的方法:

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

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

将组视为指针数组,您将失去将指针作为二维数组访问的能力

一个选项是仅使用1D阵列:

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}

当然,初始化起来不太方便。

如果将组视为指针数组,则无法将指针作为二维数组进行访问

一个选项是仅使用1D阵列:

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,那么你可以使用一个以C++为中心的解决方案,用<代码> STD::vector < /C> >和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';
}
#包括
#包括
使用std::cout;
使用std::vector;
类型定义结构我的结构{
int i;
int j;
int k;
}我的结构;
向量矩阵;
矩阵格式0{
{{0, 1, 2}, {1, 2, 3}}
};
矩阵格式1{
{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}},
{{3, 4, 5}, {4, 5, 6}, {5, 6, 7}}
};
向量组{&format0,&format1};
int main(int argc,字符**argv){
对于(自动i=groups.begin();i!=groups.end();+i)

如果你实际使用C++而不是C,那么你可以使用一个以C++为中心的解决方案,使用<代码> STD::向量< /> >和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';
}
#包括
#包括
使用std::cout;
使用std::vector;
类型定义结构我的结构{
int i;
int j;
int k;
}我的结构;
向量矩阵;
矩阵格式0{
{{0, 1, 2}, {1, 2, 3}}
};
矩阵格式1{
{{0, 1, 2}, {1, 2, 3}, {2, 3, 4}},
{{3, 4, 5}, {4, 5, 6}, {5, 6, 7}}
};
向量组{&format0,&format1};
int main(int argc,字符**argv){
对于(自动i=groups.begin();i!=groups.end();+i)


难道你对format0的初始化是错误的吗?我的结构format0[1][2]={{0,1};你是如何得到每个数组的维数的?我的意思是你从哪里得到它们的?嗨,塔默,我有一个表包含很多数字,它们彼此有内在的关系,维数是从那个表中得到的。你对format0的初始化是错误的,我的结构format0[1][2] = {{0, 1}};每个数组的维数是如何计算的?我的意思是从哪里得到的?嗨,塔默,我有一张表,里面有很多数字,它们之间有内在的联系,维数是从那张表中得到的。关于矩阵使用结构而不是简单数组的另一个建议当然更安全,也更可取。谢谢,你的意思是当我将n数组作为元素放入另一个数组中时,n数组将衰减为指针并失去其维数,对吗?关于对矩阵使用结构而不是简单数组的其他建议之一当然更安全,更可取。谢谢,你的意思是,当我将数组作为元素放入另一个数组中时,数组将衰减为指针并失去其维数是吗?嗨,塔默,谢谢你的回答;我还想定义另一个结构来容纳我需要的所有东西,你的回答证实了我的想法。你将无法访问arr[j][k]就像那样,它只是一个指针。你需要一个函数来计算arr的线性索引。@Jonathan:是的,我可以,数组实际上是指针,你可以始终使用
[]
操作符和指针。没错,既然你将它编辑为我的结构**,现在你可以访问arr[][]>但是,现在初始化将不起作用,因为它将format0视为指针数组,事实并非如此。是的,但我假设
format0、format1等也都是指针,因为他应该读取维度,从而动态分配这些数组,否则程序似乎没有用。嗨,TAMER,谢谢你r回复;我还想定义另一个结构来保存我需要的所有内容,你的回答证实了我的想法。你不能像那样访问arr[j][k],它只是一个指针。你需要一个函数来计算arr的线性索引。@Jonathan:是的,我可以,数组实际上是指针,你可以一直使用
[]
带指针的运算符。True,由于您将其编辑为my_struct**,现在您可以访问