What is different between two codes of for loop?

  auto, c++, range-based-loop, reference
#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;
}
#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;
}

Two codes above prints same values (1, 2, 3, 4, 5).
But is there different thing between initializing &x and x?
Thanks for reading!

Source: Windows Questions C++

LEAVE A COMMENT