auto 关键词 C++11

16 篇文章 1 订阅
12 篇文章 2 订阅

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)
输出结果如下:

  1. a type is : i
  2. b type is : c
  3. c type is : PKc
  4. d type is : NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
  5. e type is : 7QString
  6. f type is : d
  7. g type is : d
  8. h type is : f
  9. i type is : Pi
  10. j type is : PPKc
  11. k type is : Pd
  12. l type is : Pi
  13. n type is : Pi
    二、auto的使用场景。
    2.1 auto在循环中使用
  14.   // 初始化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.	    }
  1. // 使用auto遍历map ,
    
  2. // 你会发现你根本不需要关心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.不允许为函数的参数使用

  1. void test(auto i){ // error: ‘auto’ not allowed in function prototype
  2. }
    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无法推导出模板参数

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七 六 伍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值