Modern C++(一)auto自动类型推导

从今天开始,我会将自己一直以来使用C++11的新特性记录下来。作为自己职业生涯的记录以及经验分享。

第一篇我给了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的更多便利用法我们后面再说~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值