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 :).
If we speak about c++ and laziness
cin >> x[i]
is the answerBut I heard a lot about
scanf
faster thancin
, right?Well, it depends.
It we use
cin.tie(0)
andios_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
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 easily
About 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
well i find this more lazy for(int i = 1 ; i <= n ; scanf("%d",arr + (i++)));
I'm more lazy...
In
C++
in library<cmath>
you can use functionhypot(double x, double y)
to compute distence between 2-points instead of this code:You know when you have to print an M x N grid that is stored in an array?
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.