[英]how to declare and use right an std::array in C++
I have this two-dimensional array : array <array <int , 4> , 4> myarray
我有这个二维数组:
array <array <int , 4> , 4> myarray
How do I declare it right in a program? 如何在程序中正确声明它?
//I first include
#include <array>
then I initialize it 然后我初始化
array<array<int,4>,4> myarray = {{
{1, 2, 3, 4},
{5, 6, 0, 8},
{9, 10, 11, 12},
{13, 14, 15, 7}
}};
I declare my functions: 我声明我的职能:
void func1(array<array<int,4>,4> );
void func2();
void func3(int&, int&, array<array<int,4>,4>);
void func4(array<array<int,4>,4>);
void func5(array<array<int,4>,4>);
void func6(array<array<int,4>,4>);
write a main function: 写一个主要功能:
int main() {
func1(myarray);
func2();
func3(myarray);
func4(myarray);
func5(myarray);
func6(myarray);
return EXIT_SUCCESS;
}
Then I write the functions 然后我写函数
void func1(array<array<int,4>,4> myarray)
{//something
}
void func2();
{//something
}
void func3(int&, int&, array<array<int,4>,4> myarray);
{//something
}
void func4(array<array<int,4>,4> myarray);
{//something
}
void func5(array<array<int,4>,4> myarray);
{//something
}
void func6(array<array<int,4>,4> myarray);
{//something
}
Am I somewhere wrong ? 我在哪里错了?
The point is, that the initialized array : {1, 2, 3, 4}, {5, 6, 0, 8}, {9, 10, 11, 12}, {13, 14, 15, 7}
will be input in the first func . 关键是,初始化后的数组
{1, 2, 3, 4}, {5, 6, 0, 8}, {9, 10, 11, 12}, {13, 14, 15, 7}
将是在第一个函数中输入。 The output of the first func will be a different 4x4 array,for example {15, 14, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {2, 1, 4, 13}
. 第一个函子的输出将是一个不同的4x4数组,例如
{15, 14, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {2, 1, 4, 13}
。 This different array will be put as input in the second.The output of this func will be something different etc,etc.... 这个不同的数组将作为第二个输入。此func的输出将有所不同,以此类推...
The problem that I have is that the output of most functions is the initialized array. 我的问题是大多数函数的输出都是初始化的数组。
Thanks for the help in advance. 我在这里先向您的帮助表示感谢。
The process goes like this. 这个过程是这样的。 You initialize your array:
您初始化数组:
array<array<int,4>,4> myarray = {{
{1, 2, 3, 4},
{5, 6, 0, 8},
{9, 10, 11, 12},
{13, 14, 15, 7}
}};
Then, in main
, you call func1
. 然后,
main
调用func1
。 The implementation of funct1
is: funct1
的实现是:
void func1(array<array<int,4>,4> myarray)
{//something
}
Since you declared func1
as void
that means that it does not return a value. 由于您将
func1
声明为void
,这意味着它不会返回任何值。 array<array<int,4>,4> myarray
is type of the argument of function func1
. array<array<int,4>,4> myarray
是函数func1
的参数类型。 When you call function func1
from main
, the variable you pass from main
function will be copied into the myarray
variable of func1
(this is called pass by value), and logically, it is totally new array. 从
main
调用函数func1
时,将从main
函数传递的变量将复制到func1
的myarray
变量中(这称为按值传递),从逻辑上讲,它是一个全新的数组。 If you want to change values of your function arguments you can use references or pointers. 如果要更改函数参数的值,可以使用引用或指针。 If so, implementation of your function will be:
如果是这样,您的函数的实现将是:
void func1(array<array<int,4>,4> &myarray)
{//something
}
The &
symbol means that myarray
is reference of the variable you passed to the function. &
符号表示myarray
是您传递给函数的变量的引用。 So if, in function func1
, you modify values of myarray
, then array in your main
function will be also modifed (because you modified the values in function func1
). 因此,如果您在
func1
函数中修改了myarray
值,那么main
函数中的array也将被修改(因为您修改了func1
函数中的值)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.