Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

HitmanBBa's blog

By HitmanBBa, 9 years ago, In English

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 :).

Tags c++
  • Vote: I like it
  • +13
  • Vote: I do not like it

»
9 years ago, # |
  Vote: I like it +20 Vote: I do not like it

If we speak about c++ and laziness cin >> x[i] is the answer

  • »
    »
    9 years ago, # ^ |
    Rev. 4   Vote: I like it 0 Vote: I do not like it

    But I heard a lot about scanf faster than cin, right?

    • »
      »
      »
      9 years ago, # ^ |
      Rev. 2   Vote: I like it +9 Vote: I do not like it

      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

    • »
      »
      »
      9 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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

»
9 years ago, # |
  Vote: I like it +10 Vote: I do not like it

well i find this more lazy for(int i = 1 ; i <= n ; scanf("%d",arr + (i++)));

  • »
    »
    9 years ago, # ^ |
      Vote: I like it +9 Vote: I do not like it

    I'm more lazy...

    for (int i = 0; n-i; scanf("%d", arr+i++));
    
»
9 years ago, # |
  Vote: I like it +11 Vote: I do not like it

In C++ in library <cmath> you can use function hypot(double x, double y) to compute distence between 2-points instead of this code:

#include <cmath>

using namespace std;

double distence (double x, double y)

{

    return sqrt(x * x + y * y);

}
»
9 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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.