[英]Multidimensional array in Multidimensional array index C++
I'm implementing an translating a Python
code to C++
, however I want to place a Multidimensional array at another Multidimensional array index. 我正在实现将
Python
代码转换为C++
,但是我想将多维数组放置在另一个多维数组索引处。 The values of first array should be copied to specific index of other 2D array. 第一个数组的值应复制到其他2D数组的特定索引。
Here's my Python Code: 这是我的Python代码:
var2 = "121"
a = [None for x in xrange(3)]
a[0] = [3,4,5],[6,4,7]
a[1] = [3,9,6],[7,8,7]
a[2] = [4,8,7],[7,6,7]
for x in var2:
print 'Value:',x,'\t'
Here's my C++ Code: 这是我的C ++代码:
int a1[2][3] = {
{3,4,5},
{6,4,7}
};
int a2[2][3] = {
{3,9,6},
{7,8,7}
};
int a3[2][3] = {
{4,8,7},
{7,6,7}
};
Now I have an array: 现在我有一个数组:
int a[3][];
I want to copy a1 to a[0][0]
as in my python code. 我想像我的python代码一样将a1复制到
a[0][0]
。
My Question is, How can I copy a1,a2,a3 in array a such that: 我的问题是,如何复制数组a中的a1,a2,a3使得:
a[0][0] = a1;
a[1][1] = a2;
a[2][2] = a3;
Also, the respective for-loop
for an array index. 同样,数组索引的各个
for-loop
。
One way to directly initialize the array to hold the data is : 直接初始化数组以保存数据的一种方法是:
int a[3][2][3] = {
{{3,4,5},
{6,4,7},},
{{3,9,6},
{7,8,7},},
{{4,8,7},
{7,6,7}}
};
for( int i=0; i<3; i++ ){
for( int j=0; j<2; j++ ){
for( int k=0; k<3; k++ ){
cout<<a[i][j][k]<<'\t';
}
cout<<endl;
}
cout<<endl<<endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.