C/C++的各种骚操作

1.C风格字符串头指针p复制到q中

1
while(*q++ = *p++);

2.while循环趋近于0的写法🐶

1
2
3
while(n --> 0) {
// 实际上也就是 -- >
}

3.数组下标

1
2
3
4
5
6
int arr[] = {1,2,3,4,5,6};
for(int i = 0; i < 6; i++) {
cout << arr[i] << endl;
//cout << i[arr] << endl;
//以上两种写法等价
}

4.两种高段位判断是否为回文数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int isRev_1(int i) {
int rev = 0;
int tmp = i;

while (tmp) {
rev *= 10;
rev += tmp % 10;
tmp /= 10;
}

return rev == i;
}

int isRev_2(int i) {
#define REV_TBL_SIZE 5
static int tbl[][REV_TBL_SIZE] = {
{1000000001, 100000010, 10000100, 1001000, 110000},
{ 100000001, 10000010, 1000100, 101000, 10000},
{ 10000001, 1000010, 100100, 11000, 0},
{ 1000001, 100010, 10100, 1000, 0},
{ 100001, 10010, 1100, 0, 0},
{ 10001, 1010, 100, 0, 0},
{ 1001, 110, 0, 0, 0},
{ 101, 10, 0, 0, 0},
{ 11, 0, 0, 0, 0},
{ 1, 0, 0, 0, 0},
{ 0, 0, 0, 0, 0},
};

if (i < 0) {
i = -i;
}

int *tbl_p = (int *) tbl;
while (i < *tbl_p) {
tbl_p += REV_TBL_SIZE;
}

int tbi, mask;
for (
tbi = 0;
tbi < REV_TBL_SIZE && (mask = tbl_p[tbi]);
++tbi
) {
if (mask != 1000000001 && i >= 5 * mask) {
i -= 5 * mask;
}
if (i >= 2 * mask) {
i -= 2 * mask;
}
if (i >= mask) {
i -= mask;
}
if (i >= mask) {
i -= mask;
}
if (i & ((1 << tbi) - 1)) {
return 0;
}
}

return !i;
}

5.错误兼容抖机灵

1
2
3
4
#define ture true 
#define mian main
#define return reutrn
#define viod void

6.快速乘

1
2
3
4
5
6
7
8
9
10
11
// x * y % p
long long multiply(long long x,long long y,long long p)
{
long long ret = 0;
for( ; y; y >>= 1)
{
if(y & 1)ret = (ret + x) % p;
x = (x + x) % p;
}
return ret;
}

7.abs的高段位写法

1
2
3
4
5
6
7
8
int abs1(int x) {
int s[] = {-x,x}
return s[x > 0];
}

int abs2(int x) {
return (x ^ (x >> 31)) - (x >> 31);
}

8.类型转换编译期检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define STATIC_CHECK(expr, msg) \
{\
class ERROR_##msg {} ;\
(void) sizeof((CompileTimeChecker<(expr) != 0> (ERROR_##msg())));\
}
template <bool> struct CompileTimeChecker {
CompileTimeChecker(...);
};
template<> struct CompileTimeChecker<false> { };

template <typename To, typename From>
To safe_reinterpret_cast(From from) {
STATIC_CHECK(sizeof(To) >= sizeof(From), Destination_Too_Narrow);
return reinterpret_cast<To>(from);
}
  1. Pointer Traits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class NullType;
struct EmptyType {};

template <typename T>
class TypeTraits {
private:
template <typename U> struct PointerTraits {
enum { result = false };
using PointeeType = NullType;
};
template <typename U> struct PointerTraits<U*> {
enum { result = true };
using PointeeType = U;
};

template <typename U> struct PToMTraits {
enum { result = false };
};
template <typename U, typename V>
struct PToMTraits<U V::*> {
enum { result = true };
}
public:
enum { isPointer = PointerTraits<T>::result };
using PointeeType = PointerTraits<T>::PointeeType;

enum { isMenmberPointer = PToMTraits<T>::result };
};
script>