C++ 0X学习:auto关键词
auto并不是一个新关键词,是一个把旧关键词(貌似都没人用它了)赋予新的作用,新的作用修饰变量声明,指示编译器根据变量的初始化表达式推导变量应有的类型。auto 声明的变量必须“在声明处完成初始化”,编译器才可根据初始化表达式推导变量的类型。
二话不说,先看看例子呗。
对于现在,我们一般通过迭代器 for 容器的时候,都会写出这样的代码
#include <map> #include <string> struct people { int age; char sex; }; typedef std::map<std::string, people> people_map; int main() { people_map my_map; for (people_map::iterator it = my_map.begin(); it != my_map.end(); ++it) { /** * do some thing */ } return 0; }
获取容器的迭代器之前,我们要写出容器的类型,在这里用了 typedef,把类型缩短了=,=,要是懒点的话,很可能写成 std::map<std::string, people>::iterator。我的天啊,我仅仅想获取它的迭代器,却要写那么多代码。很多时候,我们并不需要知道容器的类型,或者,我们已经非常清楚类型了,那么,除了用 typedef 简化,还有啥方法呢?
这就是 auto 关键词的新作用了,用来推倒表达式的应有类型,代码改进后是这样子了。
#include <map> #include <string> struct people { int age; char sex; }; int main() { std::map<std::string, people> my_map; for (auto it = my_map.begin(); it != my_map.end(); ++it) { /** * do some thing */ } return 0; }
在这里,把 typedef 咔嚓掉了(但是声明变量的时候麻烦了点)=,=,然后获取迭代器,直接用 auto 了,不用管它是什么类型了。
在这里,it 自动推导成 std::map<std::string, people>::iterator 了。
auto 是不是很强大的一个关键词呢,在新的作用下,可以写出更简洁的代码。
但是 auto 也有局限性,比如上面的例子,他是推导成 std::map<std::string, people>::iterator,不会推导成 std::map<std::string, people>::const_iterator。
关于 auto 的使用,还有一些细节问题,才开始学习用,还没弄得明白
使用 auto关键词也有注意的地方(摘自网络):
注意一:auto 不能做为模板参数。因为这违背了 auto 需要由初始化表达式来推导类型的原则。
注意二:auto 不能做为函数的参数类型和返回类型。同样是因为违背了 auto 推导类型的原则。函数在编译时要实例化,此时便需要确定参数的类型,以方便安排内存。声明为 auto 的话如何确定其类型呢?没法确定,所以这样用是不允许的。
2018年5月22日 14:34
auto 不能做为模板参数。因为这违背了 auto 需要由初始化表达式来推导类型的原则。!!
2018年7月12日 10:40
I have many dreams, I like that. And I will do them gradually. Although throughout the contest she was not one of the prominent candidates, but in all the contest she will do very well and convincingly.
2018年7月16日 12:00
感谢您分享帖子! 这是我需要找到的。
2018年7月17日 11:35
I am grateful to have opened this discussion. This question is quite interesting to me. Finally the answer was found
2018年8月29日 17:10
When I'm sad I usually play games. It will make me more comfortable. I like to spend time like that
2018年9月25日 16:09
Thank you for sharing the article! Finally find this post. It's very helpful.
2018年10月02日 11:36
Nice to read the information here. The content of this post is very useful and informative
2018年10月08日 15:08
This is amazing post. Thanks for sharing.
2018年10月08日 15:08
This is amazing post. Thanks for sharing.
2018年11月26日 15:39
A guide is really helpful to me. I need such information. Thank you for sharing.
2018年12月10日 16:54
It is easy to understand, detailed and meticulous! I have had a lot of harvest after watching this article from you! I feel it interesting, your post gave me a new perspective! I have read many other articles about the same topic, but your article convinced me! I hope you continue to have high quality articles like this to share with veryone!
2018年12月28日 18:13
Treadmills are among the most famous kinds of cardio fitness hardware for both exercise centre and home utilize.
2018年12月29日 10:04
The content is quite elaborate and detailed, but I think it needs more practical elements to attract the readers' attention, thank you for sharing this article.
2019年1月05日 16:00
Summer is easy with heavy rain but I like it. It's wonderful to have rainbows after rain, and I watch it until it disappears. It seems to be free but it helps me feel comfortable
2019年1月10日 16:07
Nice to read the information here. The content of this post is very useful and informative
2019年1月14日 10:14
I understand what you bring it very meaningful and useful, thanks.
2019年1月25日 10:41
I am very happy to read this. Appreciate your sharing
2019年1月25日 10:42
Very interesting! Thanks you
2019年1月25日 10:42
You’ve written nice post, I am gonna bookmark this page, thanks for info. I actually appreciate your own position and I will be sure to come back here.
2019年3月07日 10:04
Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this
2019年3月26日 20:20
Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as
2019年5月25日 04:54
Very informative blog, . I really liked it and you
have mentioned almost all the possible ways, will try it
2019年6月18日 13:21
I was very impressed by this website. It was very appreciable, keep posting more on this. Thank you for sharing this beautiful content.
2019年7月17日 16:22
Hi my loved one! I wish to say that this article is awesome, nice written and include almost all significant infos.I'd like to see more posts like this.
2019年11月17日 20:01
Very interesting! Thanks you
2019年12月08日 14:17
very good
thank you
2019年12月08日 14:18
I like it
perfect
2019年12月17日 12:58
Very interesting! Thanks you
2019年12月17日 12:59
Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this
2019年12月17日 13:01
I was very impressed by this website. It was very appreciable, keep posting more on this. Thank you for sharing this beautiful content.
2019年12月17日 13:02
Nice to read the information here. The content of this post is very useful and informative
2020年10月09日 12:14
It is wonderful to be here with everyone, I have a lot of knowledge from what you share, to say thank you, the information and knowledge here helps me a lot
2020年11月26日 19:38
A guide is really helpful to me. I need such information. Thank you for sharing.