2012-01-25 131 views
2

我的问题很难描述,而且我有两个分别包含很多数字的表格;为一个表,我通过搜索索引格式二维数组阵列

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

我有一个包含由format分组资源多个2D阵列,每个阵列具有用于不同尺寸不同的format,划分为index,由configure type所调用,如0,1,2..6,所以我想把它们放到另一个1d数组中以便通过索引轻松查找,最后得到资源,但是我不知道怎么样。

我曾尝试以下,但失败:

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

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

+0

您对format0的初始化错误,my_struct format0 [1] [2] = {{0,1}}; –

+0

你如何拥有每个阵列的尺寸?我的意思是你从哪里得到他们? –

+0

您好TAMER,我有一个桌子上有很多数字,它们之间有内在的关系,尺寸是从那张桌子上得到的 – tiplip

回答

0

假定有两个数组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的尺寸,这是不可能的后来知道他们在数组定义之后,因此你将无法搜索(就像你说的你想要的那样)。

+0

您好,TAMER,谢谢您的回复;我也想过定义另一个结构来保存所有我需要的东西,你的回答证实了我的想法。 – tiplip

+0

你将无法像这样访问arr [j] [k],它只是一个指针。您将需要一个函数来计算arr中的线性索引。 – Jonathan

+0

@Jonathan:是的,数组实际上是指针,你可以随时在指针中使用'[]'运算符。 –

0

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

我会使用这样的:

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

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

0

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

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

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} 

当然,这是很不方便进行初始化。

+0

关于为矩阵使用结构而不是简单数组的其他建议之一肯定是更安全和首选的。 – Jonathan

+0

谢谢,你的意思是,当我将它们放入另一个数组作为元素时,数组将会衰减为指针并丢失其维度,对吧? – tiplip

0

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