c++11笔记

1、auto 和 decltype 关键字

在以前版本的C++中,定义变量必须要给出变量的类型,C++ 11利用auto关键字进行类型判断,编译器可以根据初始化代码推断出变量的类型。在使用模版时,如果某个变量依赖模版的类型参数,只能用auto确定该变量类型。

for (auto itr = myvec.cbegin(); itr != myvec.cend(); ++itr)
auto i = 10;

另外,decltype可以用来在编译期决定一个表达式的类型,比如:

int someInt;

decltype(someInt) otherIntegerVariable = 5;

 

2、遍历容器

void f(vector<double>& v)
{
	for (auto x : v) cout << x << '\n';
	for (auto& x : v) ++x;	// using a reference to allow us to change the value
        for (const auto x : { 1,2,3,5,8,13,21,34 }) cout << x << '\n';
 }


3、在C++11中能识别
list<vector<string>> lvs;

4、列表初始化
用花括号初始化变量,如
int i = {0};
int i{0};
vector<int> v1{1,2,3,4,5};
vector<int> v1 = {1,2,3,4,5}

5、constexpr和常量表达式
常量表达式指值不会改变且在编译过程就能得到计算结果的表达式。
变量声明为constexpr类型以便由编译器来验证变量的值是否为一个常量表达式。
posted @ 2014-09-12 22:10  Skyline_z  阅读(189)  评论(0编辑  收藏  举报