我很好奇为什么我们要这样处理多维数组
for (auto& row : mat1)
{
for (int& elements : row)
{
cin >> elements;
}
}
Run Code Online (Sandbox Code Playgroud)
当我们创建一个名为 elements 的临时变量来遍历行并获取每个值时,为什么要使用 &(pass by ref)。以及 for (auto& row : mat1) 中的 & ,有人可以解释这整个部分的每个细节吗?
using namespace std;
int main()
{
const int row{ 3 }, col{ 3 };
array<array<int, col>, row > mat1;
cout << "Enter matrix 1 elements: ";
for (auto& row : mat1)
{
for (int& elements : row)
{
cin >> elements;
}
}
for (auto& row : mat1) …Run Code Online (Sandbox Code Playgroud)