C++11 std - auto

时间:2014-02-17 15:41:34   收藏:0   阅读:468

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

#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.

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!