本文英文版来自这里点击打开链接
中文版来自这里点击打开链接
C++ Has Become More Pythonic
Literals字面量
Python 早在 2.6 版本中就支持将二进制作为字面量了, 最近 C++14 逐步成熟,刚刚支持这么干:
static const int primes = 0b10100000100010100010100010101100;
更不用说 Python 在 1.5 时代就有了 raw string literals 的概念,咱们 C++ 也不算晚,C++11里也有了类似做法:
const char* path = R"(C:\Python27\Doc)";
Range Loop
Python 写 for
循环是一件非常舒畅的事情:
for x in mylist:
print(x);
大家都知道了,C++11里我总算也能做同样的事情了:
for (int x : mylist)
std::cout << x;
auto类型自动推导
Python 中真的有类型的概念吗?(笑
x = "Hello World"
print(x)
C++11 也学会了这招,只不过保留了老太太的裹脚布(auto
)。
auto x = "Hello World";
std::cout << x;
tuple元组
Python 里的元组(tuple
)让人羡慕已久,这玩意 Python 从一开始就有了。
triple = (5, "Hello", True)
print(triple[0])
好嘛,我来用 C++11 照猫画虎:
auto triple = std::make_tuple(5, "hello", true);
std::cout << std::get<0>(triple);
有人说了,Python 大法好,还能逆向解析成变量呢
x, y, z = triple
哼,C++难道不行?
std::tie(x,
std::ignore
, z) = triple;
Uniform Initialization统一初始化
Python 里,Lists 是内置类型4,创建一个 list 无比简单:
mylist = [1, 2, 3, 4]
mylist.append(5);
以前我们可以说,这有啥,std::vector
差不多也能干这事。可 Python 粉较真了,您能像上面那样初始化吗?这话让 Bjarne Stroustrup 老爹听到了,暗自羞愧,于是在 C++11 里整出了个initializer_list
做出回应5。
auto mylist = std::vector<int>{1,2,3,4};
mylist.push_back(5);
可人又说了,Python 里创造个 Dictionary 简单的跟什么一样6。
myDict = {5: "foo", 6: "bar"}
print(myDict[5])
切,C++ 本身就有 map
类型,现在又多了个哈希表 unordered_map
,更像了:
auto myDict = std::unordered_map<int, const char*>{ { 5, "foo" }, { 6, "bar" } };
std::cout << myDict[5];
Lambda 表达式
Python 祭出大神器,1994年就有的 Lambda 表达式:
mylist.sort(key = lambda x: abs(x))
C++11 开始了拙劣的模仿:
std::sort(mylist.begin(), mylist.end(), [](int x, int y){ return std::abs(x) < std::abs(y); });
而 Python 在 2001 年加了一把力,引入了 Nested Scopes 的技术7:
def adder(amount):
return lambda x: x + amount
...
print(adder(5)(5))
C++11 不甘示弱,整出了 capture-list 的概念8。
auto adder(int amount) {
return [=](int x){ return x + amount; };
}
...
std::cout << adder(5)(5);
注释:函数返回值的推导在C++14中才能使用。
新的内置算法
Python 里有诸多内置的强大算法函数,如 filter
:
result = filter(mylist, lambda x: x >= 0)
C++11 倒也可以用 std::copy_if
干同样的事情:
auto result = std::vector<int>{};
std::copy_if(mylist.begin(), mylist.end(), std::back_inserter(result), [](int x){ return x >= 0; });
这样的函数在 <algorithm>
中屡见不鲜,而且都在与 Python 中的某种功能遥相呼应:transform
,any_of
,all_of
,min
,max
.
可变参数
Python 从一开始就支持可变参数了。你可以定义一个变参的函数,个数可以不确定,类型也可以不一样。
def foo(*args):
for x in args:
print(x);
foo(5, "hello", True)
C++11 里 initializer_list
可以支持同类型个数可变的参数(C++ Primer 5th 6.2.6)。
void foo(std::initializer_list<int> il) {
for (auto x : il)
std::cout << x;
}
foo({4, 5, 6});
更多Pythonic的东西见这里
点击打开链接