首页 > 编程语言 > 详细

C++11 std - auto

时间:2014-02-17 15:41:34      阅读:461      评论:0      收藏:0      [点我收藏+]

下面是倒序输出字符串的代码:

#include <stdio.h>
#include <cstdlib>
#include <string>
#include <iostream>
#include <thread>
#include <array>

using namespace std;

int main(int argc, char *argv[])
{
	freopen("temp.txt","w",stdout);
	string str("hello world");
	//C++98的写法
	//for (string::iterator i=str.begin(); it!=str.end(); ++i)

	//C++11的写法
	for(auto i = str.crbegin(); i!=str.crend();i++)
	{
		cout<<*i;
	}
	while(1);
    return EXIT_SUCCESS;
}

输出结果:

dlrow olleh

分析:

line 15 明显比 line 18行简便

结论:

auto 的主要功能就是是代码更加简洁。下面这段话是出自 《The C++ Standard Library Second Edition》

Using auto is especially useful where the type is a pretty long and / or complicated expression.

C++11 std - auto

原文:http://blog.csdn.net/drivermonkey/article/details/19301975

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!