简体   繁体   English

for循环c ++中的“冒号”和“自动”? 需要一些帮助来理解语法

[英]'colon' and 'auto' in for loop c++? need some help understanding the syntax

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'?具体来说,我对“:”和“自动”的用法感到困惑?

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 不适用于该用例。

As described in the answer by Chad, your for-loop iterates over your vector , using its begin and end iterators.如 Chad 的回答中所述,您的 for 循环使用其beginend迭代器迭代您的vector That is the behavior of the colon : syntax.这就是冒号:语法的行为。

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 * .表达式*i是(引用)容器中元素的类型: Device * This is the deduced type of auto .这是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:因为您已将const &附加到您的auto ,所以变量ioDev是对推导类型(指针)的const引用,就好像它是这样声明的:

const Device *& ioDev = *i;

It seems needlessly complicated;它似乎不必要地复杂; if you need just normal iteration (and not eg 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)

The "new" for loop simply iterates over all the elements of deviceList . “新” for循环简单地遍历的所有元素deviceList In each iteration of the body of the loop, ioDev is a const reference to each of the elments of deviceList , successively.在循环体的每次迭代中, ioDev是一个常量引用每个elments的deviceList ,依次。

As to the type of ioDev : it is of type Device *const & , as you can see in the following:至于ioDev的类型:它是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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM