C11中的类型推导auto的使用

C11提供了auto这个用于类型推导的类型,它不能用于函数参数,不能用于非静态成员变量,无法定义数组和模板参数,使用auto最多的情况就是使用模板迭代器的时候

例如:

 1 // the use of auto
 2 #include <iostream>
 3 #include <map>
 4 using namespace std;
 5 
 6 int main() {
 7     map<double, double> result;
 8     result.insert(pair<double, double>(1.2, 2.2));
 9     result.insert(pair<double, double>(1.5, 2.8));
10     map<double, double>::iterator it = result.begin();
11     for(; it != result.end(); ++it) {
12         cout << it->first << "  "  << it->second << endl;
13     }   
14     // use auto
15     cout << __func__ << ",use auto ! " << endl;
16     for(auto it = result.begin(); it != result.end(); ++it) {
17         cout << it->first << "  "  << it->second << endl;
18     }   
19     return 0;
20 }

对于auto这种类型推导会不会降低效率,是不需要担心这个问题的,因为auto在编译阶段,编译器就已经帮你推倒好了变量的类型。

但是还是要慎重使用auto,因为它可能会使程序的可读性降低。

转载于:https://www.cnblogs.com/LyndonYoung/articles/5438436.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值