小编Joo*_*kyo的帖子

for循环的两个代码有什么区别?

#include <vector>
#include <iostream>
using namespace std;

int main(void)
{
    vector<int> a = {1, 2, 3, 4, 5};
    for (auto &x : a)
        cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)
#include <vector>
#include <iostream>
using namespace std;

int main(void)
{
    vector<int> a = {1, 2, 3, 4, 5};
    for (auto x : a)
        cout << x << endl;
}
Run Code Online (Sandbox Code Playgroud)

上面的两个代码打印相同的值(1、2、3、4、5)。但是初始化 &x 和 x 之间有什么不同吗?谢谢阅读!

c++ reference auto range-based-loop

2
推荐指数
1
解决办法
67
查看次数

标签 统计

auto ×1

c++ ×1

range-based-loop ×1

reference ×1