您的位置:首页 > 编程语言 > C语言/C++

C++11新特性

2016-06-02 16:25 302 查看
auto 类型推断

auto i = 42;
auto p = new foo();

for循环

模仿其他脚本语言的for结构,不用指定起点,初始值,步长和结束条件

int arr[] = {1,2,3,4,5};
for (int& e: arr)
{
…
}

std::vector<int> vec;
vec.push(1);
vec.push(2);
for (const auto& it : vec)
{
std::count << it;
}


Smart Pointers 智能指针

unique_ptr 唯一指针

shared_ptr 有点像cocos的retain()

weak_ptr 弱引用

std::shared_ptr<int> p1(new int(42));

std::shared_ptr<int> p2 = p1;

lamdas(匿名函数?)(闭包?)

int a;
[&a](std::string str)->bool{ std::count<<str;}
[要抓捕变量][参数]->[返回值]{函数体}
&表示是按引用抓捕,否则按值抓捕,如果有多个参数用,分开

EX:

[a,&b]a按值抓捕,b按引用

[this] 按值抓捕this指针

[&] 按引用抓捕外部所有变量

[=] 按值抓捕外部所有变量

[]不抓捕任何变量

if (_imageInfoQueue && !_imageInfoQueue->empty)
{
std::string fullpath;
auto found = std::find_if(imageInfoQueue->being(),_imageInfoQueue->end(),[&fullPath](ImageInfo* ptr)->bool{return ptr->asynStruct->filename == fullpath;});
if (found != _imageInfoQueue->end())
{
…
}
}

std::begin(), std::end()
auto found = std::find_if(std::begin(_imageInfoQueue), std::end(_imageInfoQueue),[&fullpath](ImageInfo* ptr)->bool{return ptr->asynStruct->filename==fullpath;});

std::move

强制类型转换, 赋值
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++