简体   繁体   English

C++:使用“auto”在二维数组上嵌套 For 循环

[英]C++: Nested For-loops over 2 dimensional array with “auto”

To loop over a 3x3 array called "a" in C++ i used the following code.为了在 C++ 中循环一个名为“a”的 3x3 数组,我使用了以下代码。

int a[3][3] {};
for(auto &b: a) {
  for(auto &c: b) {
    std::cout << c << std::endl;
  }
}

If I had to replace the "auto", I would intuitively try如果我必须更换“自动”,我会直观地尝试

int a[3][3] {};
for(int &(b[3]): a) {
  for(int &c: b) {
    std::cout << c << std::endl;
  }
}

But this does not work.但这不起作用。 Instead I figured out that the following works.相反,我发现以下工作。

int a[3][3] {};
for(int (&b)[3]: a) {
  for(int &c: b) {
    std::cout << c << std::endl;
  }
}

So the question is: Why does the latter example work?所以问题是:为什么后一个例子有效?

I thought I needed a reference to an array of length 3, but instead I need an array of length 3 containing references.我以为我需要一个长度为 3 的数组的引用,但我需要一个包含引用的长度为 3 的数组。

int &(b[3]) is equivalent to int & b[3] , ie, an array of references to int whose length is three. int &(b[3])等价于int & b[3] ,即长度为 3int引用数组 This does not correspond to the type of the elements in the array int a[3][3] .这与数组int a[3][3]中元素的类型不对应。

The elements in the array int a[3][3] are of type int[3] , ie, an array of int whose length is three .数组int a[3][3]中的元素是 int int[3] 3] 类型,即长度为 3 的int数组 By declaring instead b as int(&b)[3] you declare a reference to such a type.通过将b声明为int(&b)[3]您声明了对这种类型的引用。

If you have an array like this如果你有这样的数组

T a[N1][N2][N3];

where T is some type specifier and N1 , N2 , and N3 the sizes of the array then a reference to this array will look like其中T是某个类型说明符, N1N2N3是数组的大小,然后对该数组的引用将如下所示

T ( &ra )[N1][N2][N3]  = a;

If you need to declare a reference to the element of the array that (the element) has the type T[N2][N3] then you can write for example如果您需要声明对(元素)类型为T[N2][N3]的数组元素的引用,那么您可以编写例如

T ( &e )[N2][N3] = a[0];

Returning to your example you declared an array返回到您的示例,您声明了一个数组

int a[3][3] {};

elements of this array have the type int[3] .此数组的元素具有int[3]类型。 So to declare a reference to elements of the array you have to write因此,要声明对数组元素的引用,您必须编写

for ( int ( %row )[3] : a )

As for this declaration至于这个声明

int &(b[3])

(where the parentheses are redundant) then it declares an array of three elements with the type int & . (括号是多余的)然后它声明了一个包含三个元素的数组,类型为int & However according to the C++ Standard you may not declare an array of references.但是,根据 C++ 标准,您不得声明引用数组。

From the C++ Standard (8.3.4 Arrays)来自 C++ 标准(8.3.4 阵列)

1 In a declaration TD where D has the form 1 在声明 TD 中,其中 D 具有以下形式

D1 [ constant-expressionopt] attribute-specifier-seqopt

and the type of the identifier in the declaration T D1 is “derived-declarator-type-list T”, then the type of the identifier of D is an array type;且声明T D1 中标识符的类型为“derived-declarator-type-list T”,则D的标识符类型为数组类型; if the type of the identifier of D contains the auto type-specifier, the program is ill-formed.如果 D 的标识符的类型包含 auto 类型说明符,则程序格式错误。 T is called the array element type; T称为数组元素类型; this type shall not be a reference type, the (possibly cvqualified) type void, a function type or an abstract class type ....此类型不应是引用类型、(可能是 cvqualified)类型 void、function 类型或抽象 class 类型...。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM