【发布时间】:2020-01-18 11:27:02
【问题描述】:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> arr {12,13,14,15,16};
for (auto & x: arr)
{
++x;
cout << x << " ";
}
return 0;
}
VS
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector <int> arr {12,13,14,15,16};
for (auto x: arr)
{
++x;
cout << x << " ";
}
return 0;
}
输出保持不变,向量中的每个值都以 1 递增。但我的教科书是这么说的。 Text Book Image 当我的教科书说“x 假设向量中每个值的副本”时,这是什么意思?
这是屏幕截图中的代码输出 Code Output 第一个输出是 & 没有&的第二个输出
【问题讨论】:
-
循环后再次打印矢量。
-
除非您希望在向量中留下递增值的尾迹,否则不需要它。
标签: c++ pass-by-reference lvalue