C ++ summarize some useful tips

Continuously updated in the .....

1. output two-dimensional array

You usually output two-dimensional array like this:

for(int i = 1; i <= n; ++i) {
    for(int j = 1; j <= m; ++j)
        printf("%d ", a[i][j]);
    printf("\n");
}

But you can so that the output:

for(int i = 1; i <= n; ++i)
  for(int j = 1; j <= m; ++j)
    printf("%d%c", a[i][j], " \n"[j == m]);

10 times is not only convenient, but also very forced grid.

The first two largest 2.O (n) in the required number of columns

Use the swap to do!

for(int i = 1; i <= n; ++i) {
  int t = a[i];
  if(t > big1) swap(big1, t);
  if(t > big2) swap(big2, t); }

Very convenient.

3.for Inflection

Here is the code for inflection. Nothing Senior C ++ syntax with 11.

// before the chain to star 
for ( int TU the p-= [U], v; v = to [the p-], the p-; the p-NXT = [the p-]) 

// tree ancestors back
for (int p = u; p ; FA = P [P])

// find highbit
for (ANS = 0; X; ANS ++, X = >>. 1);

// A fast exponentiation
for (ans = 1; x; a * = a, x >> = . 1)
  IF (. 1 & X) = A * ANS;

 

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

                                                                                       L     I     N    E

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Guess you like

Origin www.cnblogs.com/lightmain-blog/p/11564406.html