在写C宏的时候,有个技巧叫做“序列迭代”。它看起来像这样:
#define CAT(a, ...) PRIMITIVE_CAT(a, __VA_ARGS__)
#define PRIMITIVE_CAT(a, ...) a ## __VA_ARGS__
#define FUNCTION(name) void name();
#define FUNCTION_TABLE(seq) CAT(FUNCTION_TABLE_1 seq, _END)
#define FUNCTION_TABLE_1(x) FUNCTION(x) FUNCTION_TABLE_2
#define FUNCTION_TABLE_2(x) FUNCTION(x) FUNCTION_TABLE_1
#define FUNCTION_TABLE_1_END
#define FUNCTION_TABLE_2_END
FUNCTION_TABLE((x) (y) (z) (e))
Run Code Online (Sandbox Code Playgroud)
序列,即 FUCTION_TABLE 的参数,将被一一处理。但是,据我所知,令牌不会在同一范围内扩展两次。因为它被“涂成蓝色”。当 FUNCTION_TABLE_2 展开时,宏 FUNCTION_TABLE_1 已经绘制好了。为什么还要扩容?
代码如下所示.我在VS,clang ++和G ++上测试过它.所有这些都表明1 << 32和1 << i(我32岁)是不同的.我看了一下组装.它看起来像编译器计算编译的结果1 << 32.我认为这种不一致应该是一个错误,或者它只是C++的另一个未定义的行为.
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << (1 << 32) << std::endl;
int i = 32;
std::cout << (1 << i) << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
clang++:
1 << 32:73832
1 << i:1
g++:
1 << 32:73832
1 << i:1
Run Code Online (Sandbox Code Playgroud) 如果我有像这样的pandas Dataframe:
>>> df = DataFrame({'A' : [5,6,3,4], 'B' : [1,2,3, 5]})
>>> df
A B
0 5 1
1 6 2
2 3 3
3 4 5
Run Code Online (Sandbox Code Playgroud)
我想在具有特定顺序的值列表中选择一些值.它可能看起来像这样:
>>> df[df['A'].select_keeping_order([3, 4, 5])]
>>>
A B
2 3 3
3 4 5
0 5 1
Run Code Online (Sandbox Code Playgroud)
我知道有一种叫做的方法isin.但它以原始顺序而不是"参数顺序"选择值.
我该怎么做?