0

I am not able to understand the difference between these two codes as they are giving me same output?

void print() {
  int v[] = {0,1,2,3,4,5};
  for(auto x : v)
    cout << x << endl;
}

and

void increment() {
  int v[] = {0,1,2,3,4,5};
  for(auto& x : v) {
    cout << x << endl;
    ++x;
  }
}
Bill Lynch
  • 72,481
  • 14
  • 116
  • 162
Subham
  • 19
  • 3
  • Do you understand what `++x` does? Do you understand reference variables? – Bill Lynch Aug 18 '20 at 05:29
  • 2
    In one, `x` is (sequentially) a copy of each value in the array. In the other, it is a reference to each value in the array. – donkopotamus Aug 18 '20 at 05:31
  • yes i know what ++x does but i dont understand what you meant by reference variables? @BillLynch – Subham Aug 18 '20 at 05:40
  • In the first example, `x` is an `int` and, in the body of the loop, gives the value of the element being acted on. Anything done to `x` will not affect an element of `v`, since it is a copy of the element. In the second, `x` is a non-`const` reference (since you haven't specified `const`) to an element of `v`, so `++x` will increment that element. – Peter Aug 18 '20 at 05:56
  • Does this answer your question? [Is it better in C++ to pass by value or pass by constant reference?](https://stackoverflow.com/questions/270408/is-it-better-in-c-to-pass-by-value-or-pass-by-constant-reference) – Moia Aug 18 '20 at 06:29

3 Answers3

1

The first code snippet will display each element of v array without any reference (i.e. their modified values won't be applied in the original v because that'll be just a copy.

On the other hand, the another snippet represents that it'll output the element first and then the element's value will get incremented whatever it is by one and the v will be altered.

Rohan Bari
  • 6,523
  • 3
  • 9
  • 30
0

In the first snippet x is a copy of an array element, in the second it's a reference. The second snippet does increment each element in the array, but you are not observing that.

The most natural way of writing the loop is

for (auto&& x : v) {

See What is the advantage of using forwarding references in range-based for loops? for an explanation.

In many ways it's a pity that the shorthand

for (x : v) {

(which would mean auto&& x : v) was not introduced into the standard.

Bathsheba
  • 220,365
  • 33
  • 331
  • 451
0

Both the code will give you the same output. But at the end of the for loop, the values of array v is different.

In the first, x is just another copy of the items in the array v. If you print again the value of the array v then it will be the same.

In the second, x is a reference of the given items in the array v. This code is printing the value and then increasing it by 1. If you print again the value of array v then all the elements in it are increased by 1.

Try the following code:

int v[] = {0,1,2,3,4,5};
cout << "Before :";
for(auto& x : v) {
    cout << x << ", ";
    ++x;
}
cout << endl << "After: ";
for (auto x: v) {
    cout << x << ", ";
}

You will get the following output:

Before: 0, 1, 2, 3, 4, 5,                                                                                                                                     
After: 1, 2, 3, 4, 5, 6,  

Basically, both of your code is having different values for the array v at the end of the for loop.

NightWatcher
  • 128
  • 10