如何做一个"每个"环在C ++认识一个数组的长度 [英] How does a "for each" loop in C++ know the length of an array

查看:147
本文介绍了如何做一个"每个"环在C ++认识一个数组的长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看下面的例子来自 http://www.cplusplus.com/doc /教程/阵列/ ,我无法弄清楚循环第二是如何工作的。怎样才能当数组结束for循环知道。如果能算出这个,为什么第一循环不使用类似的方法?这是一个数组的长度无法确定我的IM pression。我不知道如何协调这些概念。谢谢!

编辑:感谢所有伟大的答案

 的#include<&iostream的GT;
使用命名空间std;
诠释的main()
{
  INT myArray的[3] = {10,20,30};  对(INT I = 0;我3; ++ⅰ)
    ++ myArray的[I]  对于(INT ELEM:myarray中)
    COUT<< ELEM<<的'\\ n';
}


解决方案

您是一个范围,为需要知道迭代(以下简称长)提前数误解之下。事实并非如此。

这确实需要终止条件,它的形式吧!= __end ,其中 __结束= x.end() __年底结束=(X)

当然,数组不能改变其大小,所以在此一例检测结束,知道长度是相同的(长度可以从开始,结束,指针减法得到)。

由于奥利奇在评论中提到,数组的类型确实包含长度信息,和的std ::结束用于其参数数组引用,避免衰退为指针,失去了这一信息。

的定义基本上是:

 空间std
{
    模板< typename的T,为size_t N'GT;
    T *结束(T​​(安培;数组)[N])
    {
        返回的数组+ N;
        //或者如果你想成为可爱
        //返回1 [&安培;阵列]
    }
}

当在数组引用约束是一种非类型模板参数,它推断出。编写接受阵列功能时,使用你的优势。

(编译却有阵列+ N 计算内置的阵列,它不使用的std ::月底结束()为,但是会有反正内嵌后没什么区别了,看你能怎么玩同样的伎俩的编译器非常有用。)

I was looking at the following example from http://www.cplusplus.com/doc/tutorial/arrays/ and I couldn't figure out how the 2nd for loop worked. How can the for loop know when the array ends. If it can figure this out why does the first loop not use a similar approach? It was my impression that the length of an array could not be determined. I'm not sure how to reconcile these notions. Thanks!

Edit: Thanks for all the great answers!

#include <iostream>
using namespace std;
int main()
{
  int myarray[3] = {10,20,30};

  for (int i=0; i<3; ++i)
    ++myarray[i];

  for (int elem : myarray)
    cout << elem << '\n';
}

解决方案

You're under a misconception that a ranged-for needs to know the number of iterations (the "length") in advance. It does not.

It does need a termination condition, which has the form it != __end where __end = x.end() or __end = end(x).

Of course, arrays can't change their size, so in this one case detecting the end and knowing the length are equivalent (length can be gotten from begin, end, and pointer subtraction).

As Oli mentioned in the comments, the type of an array does contain length information, and std::end uses an array reference for its parameter, to avoid the decay to pointer which loses this information.

The definition is basically:

namespace std
{
    template<typename T, size_t N> 
    T* end(T (&array)[N])
    {
        return array + N;
        // or if you want to be cute
        // return 1[&array];
    }
}

When the bound in an array reference is a non-type template parameter, it is deducible. Use this to your advantage when writing functions that accept arrays.

(The compiler actually has the array + N calculation for the end built-in for arrays, it doesn't use std::end() for that. But there would be no difference after inlining anyway, and it's useful to see how you can play the same trick the compiler does.)

这篇关于如何做一个&QUOT;每个&QUOT;环在C ++认识一个数组的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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