我问了这个问题,但由于不清楚而将其删除.我有三个循环,分别产生0、1个数据.我想将它们存储为矩阵,所以我认为应该将其存储在多维数组中,我称它为Third.
for (i = 0; i < m; i++) {
for (int l = 0; l < n2; ++l) {
for (x = 0; x < all[i].n; ++x) {
f = (int)VECTOR(v2)[l];
if (a[x].vto == f) {
printf("%d", 1);
} else {
printf("%d", 0);
}
}
}
for (int y = 0; y < n1; ++y) {
for (x = 0; x < all[i].n; ++x) {
q = (int)VECTOR(v1)[y];
if (a[x].vfrom == q) {
printf("%d", 1);
} else {
printf("%d", 0);
}
}
}
printf("\n");
}
先前代码的打印结果是,我想将其存储在此表单上:
100010001111
111000000111100100010010001001
111000000000111000000000111100100100010010010001001001
110000001100000011101010010101
111100010001
我的目标是将打印结果存储在数组中
第三= [[[100] [010] [001] [111]] [[111000] [000111] [100100] [010010] [001001]] … [[111] [100] [010] [001] ]]
因此,如果我要访问数组Third,则执行Third [3] = [[110000] [001100] [000011] [101010] [010101]]或Third [3] [1] = [001100]或Third [3] [1] [2] = 1.
我很高兴澄清所有不清楚的地方.感谢帮助
编辑1:该代码太大,无法完全添加.我询问了实现我所寻找的一般方法,即如何构造多维数组或数组数组来存储来自任何三个嵌套循环(不一定是我的循环)的数据结果.
我固定了当前代码,所以现在数组Third包含了所有数据,但是我认为这不是存储数据以便随后访问它的正确方法,因为Third [3] = [[110000] [001100] [000011] ] [101010] [010101]]
这是我所做的:
int second[n1 + n2][n1 * n2];
int first[n1 * n2];
int Third[m][n1 + n2][n1 * n2];
for (i = 0; i < m; i++) {
Data *a = all[i].datas;
igraph_edge(&graph, i, &from, &to);
igraph_neighbors(&graph, &v1, from, IGRAPH_ALL);
igraph_neighbors(&graph, &v2, to, IGRAPH_ALL);
int n2 = igraph_vector_size(&v2);
int n1 = igraph_vector_size(&v1);
int f, q, x, l, y;
for (l = 0; l < n2; ++l) {
for (x = 0; x < all[i].n; ++x) {
f = (int)VECTOR(v2)[l];
if (a[x].vto == f) {
first[x] = 1;
} else {
first[x] = 0;
}
second[l][x] = first[x];
Third[i][l][x] = second[l][x];
printf("%d", Third[i][l][x]);
}
}
for (y = 0; y < n1; ++y) {
for (x = 0; x < all[i].n; ++x) {
q = (int)VECTOR(v1)[y];
if (a[x].vfrom == q) {
first[x] = 1;
} else {
first[x] = 0;
}
second[y+n2][x] = first[x];
Third[i][y+n2][x] = second[y+n2][x];
printf("%d", Third[i][y+n2][x]);
}
}
printf("\n");
}
编译之前的代码时,我得到
100010001111
111000000111100100010010001001
111000000000111000000000111100100100010010010001001001
110000001100000011101010010101
111100010001
我不确定这是否是正确的方法?谢谢
编辑2:
我想知道这样的声明是否正确?三维数组是固定大小的三维数组,还是它(根据需要)根据n1和n2是动态的,它根据i的迭代而变化.
int second[n1 + n2][n1 * n2];
int first[n1 * n2];
int Third[m][n1 + n2][n1 * n2];
for (i = 0; i < m; i++) {
Data *a = all[i].datas;
igraph_edge(&graph, i, &from, &to);
igraph_neighbors(&graph, &v1, from, IGRAPH_ALL); //initialize the vector v2 where it's values and it's size vary depending on i
igraph_neighbors(&graph, &v2, to, IGRAPH_ALL); //initialize the vector v2 where it's values and it's size vary depending on i
int n2 = igraph_vector_size(&v2); // the size changes depending on i, i.e the change from an iteration to another
int n1 = igraph_vector_size(&v1); // the size changes depending on i, i.e the change from an iteration to another
int f, q, x, l, y;
for (l = 0; l < n2; ++l) {
for (x = 0; x < all[i].n; ++x) {
f = (int)VECTOR(v2)[l];
if (a[x].vto == f) {
first[x] = 1;
} else {
first[x] = 0;
}
second[l][x] = first[x];
Third[i][l][x] = second[l][x];
}
}
}