C++11 std - auto

本文深入探讨了使用C++编程语言倒序输出字符串的方法,并详细解释了两种实现方式的区别与优势,强调了`auto`关键字在简化代码方面的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值