循环范围内是什么类型? [英] What type is in the range for loop?

查看:53
本文介绍了循环范围内是什么类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <vector>
#include <iostream>

int main()
{
    std::vector< int > v = { 1, 2, 3 };
    for ( auto it : v )
    {
        std::cout<<it<<std::endl;
    }
}

auto 向什么扩展?是扩展到 int& 还是 int ?

To what is auto expanding? Is it expanding to int& or int?

推荐答案

它扩展为int.如果您需要参考,可以使用

It expands to int. If you want a reference, you can use

for ( auto& it : v )
{
    std::cout<<it<<std::endl;
}

根据C ++ 11标准, auto 被视为简单类型说明符 [7.1.6.2],因此对它适用相同的规则.其他简单类型说明符.这意味着使用 auto 声明引用与其他没有什么不同.

According to the C++11 standard, auto counts as a simple-type-specifier [7.1.6.2], thus the same rules apply to it as to other simple-type-specifiers. This means that declaring references with auto is no different from anything else.

这篇关于循环范围内是什么类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆