c++11的新特性

1.auto的自动类型推导,用于从初始化表达式中推断出变量的数据类型。通过auto的自动类型推导,可以大大简化我们的编程工作,后边必须有值。

2.auto是通过初始化表达式进⾏行类型推导,如果没有初始化表达式,就⽆无法确定a的类型.

3.decltype :用来查询一个变量或表达式的类型 .

4.nullptr是为了解决原来C++NULL的二义性问题而引进的一种新的类型

在原来的C++NULL实际上代表的是0  ( #define   NULL   0 )

5.序列式的for循环代码如下:

#include <iostream>

using namespace std;

void main()

{

//对于数组的遍历:

int iArray[5] = { 76, 546, 57, 8, 90 };

for (auto element : iArray)

{

cout << element << endl;

}

system("pause");

}

6.Lambda表达式:Lambda表达式用于创建并定义匿名的函数对象,以简化编程工作。

语法如下:

[函数对象参数](操作符重载函数参数)->返回值类型{函数体 }

函数对象参数:可以是Lambda表达式上方出现过的变量,代表当前Lambda表达式会用到该变量,否则在Lambda表达式内无法访问该变量,当出现Lambda表达式需要多个参数的时候,彼此之间用‘,’隔开。

       [ ]内为‘=’时,代表可访问Lambda表达式上方出现过的所有变量

操作符重载函数参数: 既为当前匿名函数的参数表

返回值类型:既为当前匿名函数的返回值

函数体:既为当前匿名函数的函数体

7.代码如下图:

//#include <iostream>

//#include <vector>

//#include <list>

//#include <string>

//#include <assert.h>

//using namespace std;

//struct stFoo

//{

//int a;

//double b;

//};

decltype用来指定返回值的类型

//auto fValue = 12.4f;

  用来指示createValue返回值类型与fValue一致

//auto createValue()->decltype(fValue)

//{

//return fValue + 152;

//}

-----nullptr的作用:避免二义性-----

//void Func(int a)

//{

//cout << a << endl;

//}

//void Func(int * p)

//{

//assert(p != NULL);//断言函数

//cout << p << endl;

//}

------lambda表达式-------

//void Test(int & _iValue)

//{

//_iValue += 10;

//}

//void main()

//{

////----auto自动数据类型推导----

//auto iValue = 10;

//cout << iValue << endl;

//auto cValue = 'W';

//cout << cValue << endl;

//auto pValue = &iValue;

//cout << *pValue << endl;

//vector<int> iVector;

////vector<int>::iterator iter = iVector.begin();

//auto iter = iVector.begin();

//auto p = new stFoo;

////----decltype : 查询变量或表达式的类型----

//auto x = 3;

//decltype(x) y = x;

//cout << "x = " << x << endl;

//cout << "y = " << y << endl;

//cout << createValue() << endl;

////-----nullptr空指针关键字-----

//auto pTemp = nullptr;

//int * p1 = nullptr;

//int * p2 = NULL;

//bool equal = (p1 == p2);

//Func(0);

////Func(p1);

////Func(p2);

////-----序列式的for循环------

//int iArray[] = { 445, 67, 654, 6, 78 };

////for (int i = 0; i < sizeof(iArray) / sizeof(int);++i)

////{

////cout << iArray[i] << endl;

////}

//for (auto element : iArray)

//{

//cout << element << endl;

//}

//list<string> heroContainer = { "Tom", "Jerry",

//"IronMan", "DaBai" };

//for (auto element : heroContainer)

//{

//cout << element << endl;

//}

////------lambda表达式-------

//int iArray1[] = { 1, 2, 3, 4, 5 };

//

//for (int i = 0; i < 5; ++i)

//Test(iArray1[i]);

//

//for (auto element: iArray1)

//cout << element << endl;

//system("pause");

//}

#include <iostream>

#include <algorithm>

using namespace std;

void main()

{

//------lambda表达式的用法-----

auto iValue1 = 10;

auto iValue2 = 100;

int iArray[5] = { 1, 2, 3, 4, 5 };

for_each(&iArray[0],&iArray[5],

[=](int & _iValue)->void{ _iValue += iValue1; _iValue += iValue2; }

);

for (auto element : iArray)

cout << element << endl;

//-------获取一个lambda表达式的返回值-------

int(*pFunc)(int,int) = nullptr;

pFunc = [](int a,int b)->int{return a +b; };

auto iResult = pFunc(3, 5);

cout << "lambda表达式的返回值为:" << iResult << endl;

system("pause");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值