C++ - Weird thing, input got reversed -
i found weird, tested code:
#include <iostream> using namespace std; int main() { int i=0, a[5]; cin>>a[i++]>>a[i++]>>a[i++]; for(int j=0; j<i; j++){ cout<<a[j]<<endl; } }
with input:
1 2 3
and got input reversed this:
3 2 1
i thought output should same code:
#include <iostream> using namespace std; int main() { int i=0, a[5]; cin>>a[i++]; cin>>a[i++]; cin>>a[i++]; for(int j=0; j<i; j++){ cout<<a[j]<<endl; } }
anybody had experienced before? thanks.
-edit-
thanks answers!!!
cin>>a[i++]>>a[i++]>>a[i++];
is syntactic sugar for
cin.operator>>(a[i++]).operator>>(a[i++]).operator>>(a[i++]);
now, 3 calls operator>>
executed left right, 3 arguments a[i++]
can evaluated in order. let's call arguments x
, y
, z
:
cin.operator>>(x).operator>>(y).operator>>(z);
you might expect compiler substitute x
, y
, z
follows:
int& x = a[i]; i++; int& y = a[i]; i++; int& z = a[i]; i++;
but in fact, compiler give more freedom. in case, chose:
int& z = a[i]; i++; int& y = a[i]; i++; int& x = a[i]; i++;
and james kanze points out, have chosen:
int& x = a[i]; int& y = a[i]; int& z = a[i]; i++; i++; i++;
Comments
Post a Comment