Modern C++(一)auto自动类型推导
2016-10-28 23:34
387 查看
从今天开始,我会将自己一直以来使用C++11的新特性记录下来。作为自己职业生涯的记录以及经验分享。
第一篇我给了auto关键字,这是因为实际使用中,确实auto给我们开发效率提升了很多,确实使用的也算是最广。在C++11之前已经有了auto,它指代的是“一个自动存储变量的关键字,也就是申明一块临时的变量内存”,比如:auto int i = 5; 我们很少使用,因为非static的变量本身就是自动存储变量。
在C++11中,auto关键字被定义为:自动变量类型推导。
我们来看看它的用法。
我们看到auto可以自动推导出变量的类型,并且当使用auto定义变量时需要进行初始化,否则auto将无从推导类型。
我们再看几个示例
由示例我们可以得出以下结论:
当声明的变量不为指针或者引用时(e),推导将忽略const限定;
当声明变量为指针或者引用时(g,i),推导将保留const限定;
volatile限定同const
auto的典型应用,在我们需要使用迭代器遍历C++容器时,通常的写法非常繁琐。
在定义it的时候需要写出具体的类型,而使用auto的写法是这样
是不是感觉清爽很多?当然遍历容器还有更简洁的写法,以及auto的更多便利用法我们后面再说~
WEB:www.easydarwin.org
Copyright © EasyDarwin.org 2012-2016
第一篇我给了auto关键字,这是因为实际使用中,确实auto给我们开发效率提升了很多,确实使用的也算是最广。在C++11之前已经有了auto,它指代的是“一个自动存储变量的关键字,也就是申明一块临时的变量内存”,比如:auto int i = 5; 我们很少使用,因为非static的变量本身就是自动存储变量。
在C++11中,auto关键字被定义为:自动变量类型推导。
我们来看看它的用法。
//后面注释的类型为变量类型 auto a = 5; //int auto b = 0.5; //double int c = 10; auto d = c; //int auto e = b; //double auto f; //error auto g = new int(5); //int* auto* h = &a; //int*
我们看到auto可以自动推导出变量的类型,并且当使用auto定义变量时需要进行初始化,否则auto将无从推导类型。
我们再看几个示例
//后面注释的类型为变量类型 int a = 5; auto* b = &a; //int* auto& c = a; //int& const auto d = a; //const int auto e = d; //int const auto& f = a; //const int& auto& g = f; //const int& const auto* h = &a; //const int* auto* i = h; //const int*
由示例我们可以得出以下结论:
当声明的变量不为指针或者引用时(e),推导将忽略const限定;
当声明变量为指针或者引用时(g,i),推导将保留const限定;
volatile限定同const
auto的典型应用,在我们需要使用迭代器遍历C++容器时,通常的写法非常繁琐。
std::map<int, string> testMap; std::map<int, string>::iterator it = testMap.begin(); for (; it != testMap.end(); ++it) { cout << it->first << "-------" << it->second << endl; }
在定义it的时候需要写出具体的类型,而使用auto的写法是这样
std::map<int, string> testMap; for (auto it = testMap.begin(); it != testMap.end(); ++it) { cout << it->first << "-------" << it->second << endl; }
是不是感觉清爽很多?当然遍历容器还有更简洁的写法,以及auto的更多便利用法我们后面再说~
获取更多信息
邮件:support@easydarwin.orgWEB:www.easydarwin.org
Copyright © EasyDarwin.org 2012-2016
相关文章推荐
- 《Effective Modern C++》翻译--条款2: 理解auto自动类型推导
- 《Effective Modern C++》翻译--条款2: 理解auto自动类型推导
- 《Effective Modern C++》读书笔记(2) -- auto类型推导(auto type deduction)
- Effective Modern C++之Item 2 理解auto的类型推导
- C++ 11 学习1:类型自动推导 auto和decltype
- C++ 11 学习1:类型自动推导 auto和decltype
- Effective Modern C++ Item2 理解auto类型推导
- 《Effective Modern C++》翻译--条款2: 理解auto自己主动类型推导
- Effective Modern C++翻译(3)-条款2:明白auto类型推导
- C++以auto完成类型自动推导
- [effective modern c++][5]与其使用显示类型推断不如使用auto
- Effective Modern C++ Item1 模板类型推导详解
- 《Effective Modern C++》翻译--条款1: 理解模板类型推导
- [C++]高效使用c++11--理解auto类型推导
- [effective modern c++][2]理解auto类型推断
- 你好,C++(11)如何用string数据类型表示一串文字?根据初始值自动推断数据类型的auto关键字(C++ 11)
- 如何在编译时刻判断两个类型是否可以自动转换?——《Modern C++ Design》读书笔记(1)
- Effective Modern C++ 条款6 当auto会推断出不合理的类型时使用显式类型初始化语法
- c++:自动获取数据类型,auto与decltype的异同
- 《Effective Modern C++》读书笔记(1) -- 模板类型推导(template type deduction)