栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++11 range-for 中的auto,auto&, auto&&, const auto&

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++11 range-for 中的auto,auto&, auto&&, const auto&

range-for 是对一定范围内的(如容器vector或者一段string)的循环遍历语句,从头到尾。参考Range-based for loop (since C++11) - cppreference.com<

Executes a for loop over a range.

Used as a more readable equivalent to the traditional for loop operating over a range of values, such as all elements in a container. 

>

我们需要声明一个变量,叫做range-declaration(如下文中的变量c), 并且需要为其确定一个类型,通常我们用auto这个关键字 ,进行自动类型推导。

此时就涉及到标题中auto,auto&, auto&&, const auto&的使用,其实更多的是C++中值传递,引用传递,右值引用等知识,使用以下代码进行了测试,以及测试结果。

#include 
#include 
#include 
#include 
#include 
using std::vector;

int main()
{

//just auto c, 创建拷贝,每次遍历都是copy一次,不会对str中的元素进行修改
	std::string str("str ing1");
	std::cout< 

学习时用的参考链接:

C++ 遍历循环表达示 auto, auto&, auto&&

C++ 中的auto、auto&、auto&&、const auto&_橙橙小狸猫的博客-CSDN博客_auto&&

Range-based for loop (since C++11) - cppreference.com

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1021747.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号