C++11引入了auto关键词实现类型推导。这个关键词不仅能方便的获得复杂的类型,还能简化书写,提高编码效率。
我的理解是这样的。auto为系统自己会帮助我们给出变量的声明(这个太好,我们可以不那么关注变量的类型了)
我们来看看他的用法。typeid是一个可以帮助我获取数据类型的好东西。
1. auto a = 10; // auto 被推导成 int
2. auto b = 'a'; // auto 被推导成 字符
3. auto c = "10"; // auto 配推导成,常量字符组
4. string d = "10"; //
5. QString e = "10"; //
6. auto f = 1.0; // 被推到成double
7. double g = 1.0;
8. float h = 1.0;
9. qDebug()<<"a type is :"<<typeid(a).name(); // 输出结果为:a type is : i
10. qDebug()<<"b type is :"<<typeid(b).name(); // 输出结果为:b type is : c
11. qDebug()<<"c type is :"<<typeid(c).name(); // 输出结果为:c type is : PKc
12. qDebug()<<"d type is :"<<typeid(d).name(); // 输出结果为:d type is : NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
13. qDebug()<<"e type is :"<<typeid(e).name(); // 输出结果为:e type is : 7QString
14. qDebug()<<"f type is :"<<typeid(f).name(); // 输出结果为:f type is : d
15. qDebug()<<"g type is :"<<typeid(g).name(); // 输出结果为:g type is : d
16. qDebug()<<"h type is :"<<typeid(h).name(); // 输出结果为:h type is : f
17. auto i = new auto(10);
18. auto j = new auto("10");
19. auto k = new auto(1.0);
20. auto l = &a;
21. const auto n = &a;
22.
23. qDebug()<<"i type is :"<<typeid(i).name(); // 输出结果为:i type is : Pi
24. qDebug()<<"j type is :"<<typeid(j).name(); // 输出结果为:j type is : PPKc
25. qDebug()<<"k type is :"<<typeid(k).name(); // 输出结果为:k type is : Pd
26. qDebug()<<"l type is :"<<typeid(l).name(); // 输出结果为:l type is : Pi
27. qDebug()<<"n type is :"<<typeid(n).name(); // 输出结果为:n type is : Pi
其他的我相信大家都能看得懂就是PKc,我查的资料显示的P(point) K(const) c(char) d(double) f(float)
输出结果如下:
- a type is : i
- b type is : c
- c type is : PKc
- d type is : NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
- e type is : 7QString
- f type is : d
- g type is : d
- h type is : f
- i type is : Pi
- j type is : PPKc
- k type is : Pd
- l type is : Pi
- n type is : Pi
二、auto的使用场景。
2.1 auto在循环中使用 -
// 初始化map
map<int,int> resultMap;
3. for(int i = 0 ; i < 10; i ++){
4. resultMap[i] = random()%100;
5. }
6. // 未使用auto遍历map
7. map<int,int>::iterator it = resultMap.begin();
8. for(;it != resultMap.end();++it){
9. qDebug()<< it->first << "-"<< it->second;
10. }
-
// 使用auto遍历map ,
-
// 你会发现你根本不需要关心resultMap的类型是啥,如果他的类型是map<QString,int>你也可以使用下面代码遍历
for(auto it2 = resultMap.begin();it2 != resultMap.end();++it2){
14. qDebug()<< it2->first << "-"<< it2->second;
15. }
2.2 泛型函数使用
三、auto的限制
3.1.未初始化数据系统无法推导,并且在编译时报错。
auto xx; // error: declaration of variable ‘xx’ with deduced type ‘auto’ requires an initializer
3.2.显示变量有两种类型。
auto int xx2; // error: two or more data types in declaration of ‘xx2’
3.3.不允许为函数的参数使用
- void test(auto i){ // error: ‘auto’ not allowed in function prototype
- }
3.4.不能用于非静态成员变量
struct Stu{
2. auto num_ = 95275; // error: 'auto' not allowed in non-static struct member
3. static const auto age_ = 10; //OK: age_ -> static const int
4. };
3.5.auto无法定义数组
1
. int arr[10] = {0,1,2,3,4,5,6,7,8,9}; // OK:
2. auto aa = arr; // OK:
3. auto rr[10] = arr; // error: 'rr' declared as array of 'auto'
4. auto r1[10] = {0,1,2,3,4,5,6,7,8,9}; // error: 'r1' declared as array of 'auto'
5. qDebug()<<"aa type is :"<<typeid(aa).name();
结果为:
aa type is : Pi
3.6.auto无法推导出模板参数