美文网首页
6.12a For-each loops

6.12a For-each loops

作者: Closears | 来源:发表于2017-08-28 16:40 被阅读7次

    原完整教程链接:6.12a For-each loops

    1.
    /* The for-each statement has a syntax that looks like this:
    
       for (element_declaration : array)
          statement;
    */
    
    // Let’s take a look at a simple example that uses a for-each loop to 
    // print all of the elements in an array named fibonacci:
    
    int main()
    {
        int fibonacci[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
        for (int number : fibonacci) // iterate over array fibonacci
           std::cout << number << ' '; // we access the array element for this iteration through variable number
     
        return 0;
    }
    // This prints:
    // 0 1 1 2 3 5 8 13 21 34 55 89
    
    2.
    /* 
       Because element_declaration should have the same type as the 
       array elements, this is an ideal case in which to use the auto 
       keyword, and let C++ deduce the type of the array elements for us.
    */
    int main()
    {
        int fibonacci[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
        for (auto number : fibonacci) // type is auto, so number has its type deduced from the fibonacci array
           std::cout << number << ' ';
     
        return 0;
    }
    
    3.
    /*
       Each array element iterated over will be copied into variable 
       element. (此处可以按照函数利用括号传递参数的形式来理解,即把
       for (auto element: array) 想象成 function(auto element) {})
       Copying array elements can be expensive, and most of 
       the time we really just want to refer to the original element. 
       Fortunately, we can use references for this:
    */
       int array[5] = { 9, 7, 5, 3, 1 };
       for (auto &element: array) // The ampersand makes element a reference to
                                  // the actual array element, preventing a copy 
                                  // from being made
            std::cout << element << ' ';
    
    4.
    // And, of course, it’s a good idea to make your element const if 
    // you’re intending to use it in a read-only fashion:
       int array[5] = { 9, 7, 5, 3, 1 };
       for (const auto &element: array) // element is a const reference to 
                                        // the currently iterated array element
            std::cout << element << ' ';
    
    5.
    **For-each doesn’t work with pointers to an array!**
    /*
       In order to iterate through the array, for-each needs to know how 
       big the array is, which means knowing the array size. Because 
       arrays that have decayed into a pointer do not know their size, 
       for-each loops will not work with them!
    */
    #include <iostream>
     
    int sumArray(int array[]) // array is a pointer
    {
        int sum = 0;
        for (const auto &number : array) // compile error, the size of array 
                                         // isn't known
            sum += number;
     
        return sum;   
    }
     
    int main()
    {
         int array[5] = { 9, 7, 5, 3, 1 };
         std::cout << sumArray(array); // array decays into a pointer here
         return 0;
    }
    /*
       总结:在通过参数列表传递数组名的时候,数组名其实被“退化”成
       了一个指针。平时我们总认为数组名就等价于数组中第一个元素的
       指针,其实是不够准确的。数组名除了包含第一个元素的指针,还
       包含了数组的大小。因此在一些需要知道数组大小的上下文环境
       中,如for-each loop,我们通过参数列表传递的数组名被隐式地“退
       化”成了一个指针,丢掉了数组大小这个信息,便会出现编译错误。
    */
    

    相关文章

      网友评论

          本文标题:6.12a For-each loops

          本文链接:https://www.haomeiwen.com/subject/girgdxtx.html