C++ for循环c ++中的“冒号”和“自动”?需要一些帮助来理解语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35490236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'colon' and 'auto' in for loop c++? need some help understanding the syntax
提问by MIbrah
I need some explanation for the following c++ syntax:
我需要对以下 C++ 语法进行一些解释:
for(const auto& ioDev : deviceList)
given that:
鉴于:
std::vector<Device *> deviceList
Specifically, I am confused about ':' and the usage of 'auto'?
具体来说,我对“:”和“自动”的用法感到困惑?
采纳答案by anatolyg
As described in the answer by Chad, your for-loop iterates over your vector
, using its begin
and end
iterators. That is the behavior of the colon :
syntax.
正如 Chad 在回答中所述,您的 for 循环vector
使用 itsbegin
和end
iterators 迭代您的 。这就是冒号:
语法的行为。
Regarding your const auto &
syntax: you should imagine what code comes out of it:
关于你的const auto &
语法:你应该想象一下它产生了什么代码:
// "i" is an iterator
const auto& ioDev = *i;
The expression *i
is (a reference to) the type of elements in the container: Device *
. This is the deduced type of auto
. Because you have const &
appended to your auto
, the variable ioDev
is a const
reference to the deduced type (a pointer), as if it were declared this way:
表达式*i
是(引用)容器中元素的类型:Device *
。这是 的推导类型auto
。因为您已const &
附加到您的auto
,该变量ioDev
是const
对推导类型(指针)的引用,就好像它是这样声明的:
const Device *& ioDev = *i;
It seems needlessly complicated; if you need just normal iteration (and not e.g. manipulating the address of the pointer, which I think is highly unlikely), use a plain unmodified auto
:
它似乎不必要地复杂;如果您只需要正常的迭代(而不是例如操纵指针的地址,我认为这是极不可能的),请使用普通的未修改auto
:
for (auto ioDev : deviceList)
or an explicit type:
或显式类型:
for (Device* ioDev : deviceList)
回答by Chad
This is a range based for loop, it has the same basic behavior of:
这是一个基于范围的 for 循环,它具有相同的基本行为:
for(auto it = deviceList.begin(); it != deviceList.end(); ++it)
{
const auto& ioDev = *it;
}
The range based for loop has quickly become one of my favorite constructs, it's terse and when you need to iterate an entire range, works as well (and as efficiently) as possible.
基于范围的 for 循环很快成为我最喜欢的结构之一,它很简洁,当您需要迭代整个范围时,它会尽可能地(并且尽可能高效)地工作。
If you need the other constructs of a typical for loop (say to exit early in some case), then range-based for isn't for that use case.
如果您需要典型 for 循环的其他构造(比如在某些情况下提前退出),那么基于范围的 for 不适用于该用例。
回答by Ami Tavory
The "new" for
loop simply iterates over all the elements of deviceList
. In each iteration of the body of the loop, ioDev
is a const reference to each of the elments of deviceList
, successively.
“新”for
循环简单地遍历 的所有元素deviceList
。在循环体的每次迭代中,ioDev
是对 , 的每个元素的 const 引用deviceList
。
As to the type of ioDev
: it is of type Device *const &
, as you can see in the following:
至于ioDev
: 的类型是 type Device *const &
,如下所示:
#include <vector>
#include <type_traits>
using namespace std;
int main()
{
vector<int *> v;
for(const auto &r: v)
{
static_assert(is_same<decltype(r), int *const &>::value, "wrong type");
}
}