C++11特性之auto关键字

12 篇文章 2 订阅

什么是C++11

简单的来说就是2011指定的C++标准。该标准在 C++ 98 的基础上修正了约 600 个 C++ 语言中存在的缺陷,同时添加了约 140 个新特性,这些更新使得 C++ 语言焕然一新。

C++ auto类型推导

C++11赋予auto关键字新的含义,使用它可以编译器在编译期间自动推导出变量的类型。

auto关键的基本语法如下

auto name = value;

name是变量名,value是它初始化的值。
来一个auto简单的例子:

#include <iostream>

using namespace std;

int main()
{

	auto i = 10;
	auto f = 10.8;
	auto pi = &i;
	auto str = "Hello world";

	cout << "i: " << i << endl;
	cout << "f: " << f << endl;
	cout << "pi: " << pi << endl;
	cout << "str: " << str << endl;

	system("pause");
	return 0;
}

其运行结果:
在这里插入图片描述
解释一下:i给编译器推导出是int类型;f给编译器推导出是float类型; pi给编译器推导出事int*类型;str给编译器推导出是str类型。

auto高级的例子:

auto可以和某些类型混合使用,这样的auto表示“半个”类型,下如代码

#include <iostream>

using namespace std;

int main()
{
	int x = 0;
	auto *p1 = &x;  //p1为int* 类型,auto被推导为int 类型
	auto p2 = &x;   //怕为int* 类型, auto被推导为int* 类型
	auto &r1 = x;	//r1为int& 类型, auto被推导为int 类型      //这句话是取别名的意思
	auto r2 = r1;  //r2为int 类型, auto被推导为int 类型

	system("pause");
	return 0;
}

auto当然也可以与const结合:
例子:

#include <iostream>

using namespace std;

int main()
{
	int x = 0;
	const auto n = x; //n为const int,auto被推导为int

	auto f = n;  //f为const int, auto被推导为int(const属性被抛弃)为啥了下面就解散
	const auto &r1 = x;  //r1为const int& 类型, auto被推导为int
	auto &r2 = r1;  //r2为cosnt int& 类型, auto被推导为const int类型


	system("pause");
	return 0;
}

auto与const结合的用法:

  • 当类型不为引用时,auto 的推导结果将不保留表达式的 const 属性;
  • 当类型为引用时,auto 的推导结果将保留表达式的 const属性。

auto 的限制

  • 使用 auto 的时候必须对变量进行初始化。
  • auto 不能在函数的参数中使用。
  • auto 不能作用于类的非静态成员变量(也就是没有 static 关键字修饰的成员变量)中。
  • auto 关键字不能定义数组
  • auto 不能作用于模板参数

auto 的应用
前面谈了auto的规则和限制,下面举几个在实际开放中auto应用的例子。

  • 使用auto定义迭代器

auto 的一个典型应用场景是用来定义 stl 的迭代器。

我们在使用 stl 容器的时候,需要使用迭代器来遍历容器里面的元素;不同容器的迭代器有不同的类型,在定义迭代器时必须指明。而迭代器的类型有时候比较复杂,书写起来很麻烦,请看下面的例子:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	
	vector<vector<int>> v;
	vector<vector<int>>::iterator i = v.begin();

	system("pause");
	return 0;
}

使用上auto后:

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	
	vector<vector<int>> v;
	auto i = v.begin();//用auto代替具体类型

	system("pause");
	return 0;
}

auto 可以根据表达式 v.begin() 的函数返回值类型来推导出变量 i 的类型。

  • auto用于泛型编程

auto 的另一个应用就是当我们不知道变量是什么类型,或者不希望指明具体类型的时候,比如泛型编程中。我们接着看例子:
使用了auto的代码:

#include<iostream>
using namespace std;

class A
{
public:
	static int get()
	{
		return 100;
	}
};

class  B
{
public:
	static const char* get()
	{
		return "Welcome to my blog,please leave comments and likes ";
	}
};

template<typename T>
void func()
{
	auto val = T::get();
	cout << val << endl;
}

int main()
{
	func<A>();
	func<B>();
	system("pause");
}

不是使用auto的代码:

#include<iostream>
using namespace std;

class A
{
public:
	static int get()
	{
		return 100;
	}
};

class B
{
public:
	static const char* get()
	{
		return "Welcome to my blog, please leave comments and likes";
	}
};

//T1是指哪个类, T2是指哪个类型
template<typename T1, typename T2>
void func()
{
	T2 val = T1::get();
	cout << val << endl;
}

int main()
{
	//调用时给模板附上对应的类型
	func<A, int>();
	func<B, const char*>();
}

他们的运行结果都是如下图:
在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

困了就喝白茶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值