C++ 11的几个新特性

1,range for; alto

for (const auto& agent : componentAgents_)
{
    agent->interface();
}

参见 http://www.cnblogs.com/h46incon/archive/2013/06/02/3113737.html

 

最新的C++标准更新了auto关键字的功能
除了具有原有的含义外,还增加了一种类似其他高级语言的型别推导特性
使用auto来代替变量的类型,
前提是被明确类型的初始化变量初始化的,可以使用auto关键字
比如int i=10; auto a = i; //这样a也是int类型了
这在使用一些模板类的时候,对于减少冗赘的代码也很有用
参见 http://zhidao.baidu.com/link?url=6_MLuvtOysRI0Mar386KMVr4_akcjfecdhUkg14esU7wM7G3UAtoR5ngvT50zM5yqdjxzxWxtB6t4Isn3Zh1W_

2,Lanbda表达式

    // the type of a closure cannot be named, but can be inferred with auto
    auto func1 = [](int i) { return i+4; };
    std::cout << "func1: " << func1(6) << '\n'; 

[ capture ] ( params ) mutable exception attribute -> ret { body }

capture 指定了在可见域范围内 lambda 表达式的代码内可见得外部变量的列表,具体解释如下:

  • [a,&b] a变量以值的方式呗捕获,b以引用的方式被捕获。
  • [this] 以值的方式捕获 this 指针。
  • [&] 以引用的方式捕获所有的外部自动变量。
  • [=] 以值的方式捕获所有的外部自动变量。
  • [] 不捕获外部的任何变量。

参见 http://www.cnblogs.com/haippy/archive/2013/05/31/3111560.html

 

posted @ 2014-10-28 23:43  codel_1220  阅读(216)  评论(0编辑  收藏  举报