【发布时间】:2021-03-03 00:48:09
【问题描述】:
我只是在为我的程序编写一些代码,但最终我忘了写一个小符号并出错,请帮助理解这里出了什么问题。
我想写的代码-
int arr[3][2] = {with some values};
for(auto &i : arr) {
for(int j : i)
cout << j << " " ;
cout << endl;
}
我不小心写的代码-
int arr[3][2] = {with some values};
for(auto i : arr) { // just forgot to write (&) and got error
for(int j : i)
cout << j << " " ;
cout << endl;
}
我知道的事情 - 引用 ("&") 运算符使我们能够更改循环内部的值。
但是第二个循环中的错误是什么,我的意思是它可以一个一个地深度复制数组对象,然后创建单独的对象然后迭代它们,尽管我们将无法更改数组值本身。
我遇到了错误 - 'std::end' declared here 1244 | end(const valarray<_Tp>& __va)
【问题讨论】:
-
您可能遇到了数组指针衰减。指针不能用作基于范围的 for 范围表达式。
标签: c++ arrays loops c++11 foreach