### HitmanBBa's blog

By HitmanBBa, 8 years ago,

Hi everybody...

How are you ??

I have learnt a new move in c++ while reading an array, and I would like to share it with you my friends :).

Most of us while reading an array in contest he/she use this move:

int x[100005];
int i;
for(i=0;i<50;i++)
scanf("%d", &x[i]);


We use &x[i] for point to the ith element in the array... look look look we just press shift + 7 and then write x[i]. oh that's make me so tired.

But look at this move...

int x[100005];
int i;
for(i=0;i<50;i++)
scanf("%d", x+i); // I just press x + i only and for the same reason, that's make me comfortable. ^_^


If you got any lazy move like this please share it with us :).

• +13

 » 8 years ago, # |   +20 If we speak about c++ and laziness cin >> x[i] is the answer
•  » » 8 years ago, # ^ | ← Rev. 4 →   0 But I heard a lot about scanf faster than cin, right?
•  » » » 8 years ago, # ^ | ← Rev. 2 →   +9 Well, it depends.It we use cin.tie(0) and ios_base::sync_with_stdio(false) then on real g++ it's as fast as scanf, but not in MSVC, and not always in MinGW.Currently on CF its slower but is usually fast enough
•  » » » 8 years ago, # ^ |   0 You can also use my 11158242 template. It uses streams, is big enough, but is very convenient(at least for me) and can be used for user-defined types easilyAbout speed you should understand, when it is critical and when it is better to switch to stdio and even use arrays instead of stl. But I don't remember last time when it was such critical to io
 » 8 years ago, # |   +10 well i find this more lazy for(int i = 1 ; i <= n ; scanf("%d",arr + (i++)));
•  » » 8 years ago, # ^ |   +9 I'm more lazy... for (int i = 0; n-i; scanf("%d", arr+i++)); 
 » 8 years ago, # |   +11 In C++ in library  you can use function hypot(double x, double y) to compute distence between 2-points instead of this code: #include using namespace std; double distence (double x, double y) { return sqrt(x * x + y * y); } 
 » 8 years ago, # |   0 You know when you have to print an M x N grid that is stored in an array? for(int i = 0 ; i < m ; i++) for(int j = 0 ; j < n ; j++) cout << arr[i][j] << "\n"[j!=n-1]; It prints the elements with a space between them, and with an endline when j reaches n-1.But sometimes the OJ doesn't recognize the space, so be careful.