What does quot; for (const auto amp;s : strs) {} quot; mean?(什么是for (const auto amp;s : strs) {} quot;意思是?)
问题描述
for (const auto &s : strs)
是什么意思?冒号 :
的作用是什么?
What does for (const auto &s : strs)
mean? What is the function of colon :
?
vector<string> &strs;
for (const auto &s : strs){
//
}
推荐答案
它实际上是一个 C++11 特性,称为基于范围的 for 循环".
It's actually a C++11 feature called "range-based for-loops".
在这种情况下,它基本上是一个更容易编写的替代品:
In this case, it's basically an easier-to-write replacement for:
// Let's assume this vector is not empty.
vector<string> strs;
const vector<string>::iterator end_it = strs.end();
for (vector<string>::iterator it = strs.begin(); it != end_it; ++it) {
const string& s = *it;
// Some code here...
}
:
是新语法的一部分.
在左侧,您基本上有一个变量声明,它将绑定到向量的元素,而在右侧,您有一个要迭代的变量(也称为范围表达式").
On the left you basically have a variable declaration that will be bound to the elements of the vector and one the right you have the variable to iterate on (also called "range expression").
以下是解释范围表达式的先决条件的链接文档的摘录:
Here is an excerpt of the linked documentation that explains the prerequisites for the range-expressions:
range_expression 被评估以确定要迭代的序列或范围.序列中的每个元素依次被取消引用并分配给具有 range_declaration 中给出的类型和名称的变量.
range_expression is evaluated to determine the sequence or range to iterate. Each element of the sequence, in turn, is dereferenced and assigned to the variable with the type and name given in range_declaration.
begin_expr 和 end_expr 定义如下:
begin_expr and end_expr are defined as follows:
如果__range是一个数组,则begin_expr是__range,end_expr是(__range + __bound),其中__bound是数组中元素的个数(如果数组大小未知或类型不完整,程序就出错了-形成)
If __range is an array, then begin_expr is __range and end_expr is (__range + __bound), where __bound is the number of elements in the array (if the array has unknown size or is of an incomplete type, the program is ill-formed)
如果 __range 的类型是具有 begin 或 end 成员函数之一或两者的类类型,则 begin_expr 是 __range.begin() 而 end_expr 是 __range.end();
If __range's type is a class type with either or both a begin or an end member function, then begin_expr is __range.begin() and end_expr is __range.end();
否则,begin_expr 是 begin(__range) 和 end_expr 是 end(__range),它们是通过参数依赖查找找到的,std 作为关联的命名空间.
Otherwise, begin_expr is begin(__range) and end_expr is end(__range), which are found via argument-dependent lookup with std as an associated namespace.
请注意,由于所有这些,基于范围的 for 循环也支持迭代 C 数组,因为 std::begin
/std::end
也适用于这些数组.
Note that thanks to all this, range-based for loops also support iterating over C arrays as std::begin
/std::end
works with those too.
这篇关于什么是"for (const auto &s : strs) {} "意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:什么是"for (const auto &s : strs) {} &quo
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01