C++11新特性

  • 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
強制類型轉換, 賦值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章