你是否因为自己不够自卑而感到抽象?
啊不
你是否因为自己不够抽象而感到自卑?
本期文章,将教你做一个抽象的人
一、尊嘟假嘟
// C++ code.
#include <iostream>
struct 尊嘟假嘟 // 建议用英文
{
void O()
{
std::cout << "尊嘟假嘟" << std::endl;
}
} o;
struct 假嘟尊嘟 // 建议用英文
{
void o()
{
std::cout << "假嘟尊嘟" << std::endl;
}
} O;
int main()
{
o.O();
O.o();
}
# Python code.
class 尊嘟假嘟: # 建议用英文
def O():
print('尊嘟假嘟')
class 假嘟尊嘟: # 建议用英文
def o():
print('假嘟尊嘟')
if __name__ == '__main__':
o = 尊嘟假嘟()
O = 假嘟尊嘟()
o.O()
O.o()
同理,可得尊嘟尊嘟
和假嘟假嘟
二、真假变量名 / 宏
哎呀,true = 0
,false = 1
,好抽象!
// C/C++ code.
#include <iostream>
#define begin }
#define end {
#define false 1
#define true 0
#define int _
#define main __
#define return ___
_ __()
end
_ ____, _____;
___ true;
begin
# Python code.
false = True
true = False
def main():
print(f'True: {true} False: {false}')
return true, false
if __name__ == '__main__':
print(main())
三、不用加法实现加法
原本,add
是加法,sub
是减法,这里互换,是为了抽象。
这里,我挖个坑,不知何年何月何日何时何分何秒何毫秒何微秒何纳秒何......(喘气),讲解原理
// C/C++ code.
int sub(int x, int y) // 加法
{
if (x == 0)
{
return y;
}
return sub((x & y) << 1, x ^ y);
}
int add(int x, int y) // 减法
{
return sub(x + 1, ~y);
}
# Python code.
def sub(x: int, y: int) -> int: # 加法
if x == 0:
return y
return sub(x & y << 1, x ^ y)
def add(x: int, y: int) -> int: # 减法
return sub(x + 1, ~y)
四、使用趋近符号
详见《C/C++用于装逼的运算符!》
即符号-->
与<--
在while
和do-while
中的运用。
我才不告诉你a --> 1
是a-- > 1
,1 <-- a
是1 < --a
五、把乘法变成位运算
这里的乘法指的是数字乘变量,即如10 * x
。
第一步,把数字分解二进制,10(10)=1010(2)。
第二步,把1的位数(从右往左数,以0开始)列出来,(1,3)。
第三步,代码写成变量 << [列出来的数]
的和,x << 3 + x << 1
。
六、代用运算符
符号 (C++ 为关键字,C 以宏的形式定义于<iso646.h> ) |
代用 |
---|---|
&& | and |
&= | and_eq |
& | bitand |
| | bitor |
~ | compl |
! | not |
!= | not_eq |
|| | or |
|= | or_eq |
^ | xor |
^= | xor_eq |
符号 | 代用 | 注意 |
---|---|---|
{ | <% | |
} | %> | |
[ | <: | 出现<:: 时,除非<::> 被翻译成[] 、<::: 被翻译成[:: ,其余被认为是< :: ,如::std::vector<::std::string> |
] | :> | |
# | %: | |
## | %:%: |
以及
符号 (于C++17、C23移除) | 代用 | 注意 |
---|---|---|
{ | ??< | |
} | ??> | |
[ | ??( | |
] | ??) | |
# | ??= | |
\ | ??/ | // Will the next line be executed?????/ 会注释掉下一行,"Enter date ??/??/??" 会被认定成"Enter date \\??" |
^ | ??’ | |
| | ??! | |
~ | ??- |
(示例来自cppreference.com)
%:include <iostream>
struct X
<%
compl X() <%%> // 析构函数
X() <%%>
X(const X bitand) = delete; // 复制构造函数
bool operator not_eq(const X bitand other)
<%
return this not_eq bitand other;
%>
%>;
int main(int argc, char* argv<::>)
<%
// 带引用捕获的 lambda
auto greet = <:bitand:>(const char* name)
<%
std::cout << "Hello " << name
<< " from " << argv<:0:> << '\n';
%>;
if (argc > 1 and argv<:1:> not_eq nullptr)
greet(argv<:1:>);
else
greet("Anon");
%>
七、原生不要一不小心看成原神,启动!数组取下标
原生数组:type name[<可选于有赋值时,指数组长度>]<(可选) = {内容}>
name[x]
等价于 *(name + x)
等价于 *(x + name)
等价于 x[name]
也就是 1[a]
这种奇怪的写法
注意,必须是原生数组!
再提一句,(见第五条),1<:a:>
b站上刚刷到过你是否想说:bilibili
不知道是否知名up @Erik_Tse