【发布时间】:2020-04-19 03:16:44
【问题描述】:
我有一个 int 类型数组,我想使用基于范围的 for 循环来引用它的值,但是我找不到使用 for(auto& x:A) 和 for(auto &x:A) 之间的区别。两者在我的编译器中都有效并给出相同的输出。有区别吗?
这是我正在使用的代码和输出:
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int A[10] = {0,1,2,3,4,5,6,7,8,9};
//Using any of the two following lines, there seems to be no diference in the output.
//for (auto& x : A) x=1;
//for (auto &x : A) x=1;
for (auto x : A) cout << x << " ";
}
输出是:
1 1 1 1 1 1 1 1 1 1
【问题讨论】: