简体   繁体   English

这是在C ++中使用for循环的正确方法吗?

[英]Is it the correct way to use for loop in C++?

I seen weird for loop syntax in C++. 我在C ++中看到了怪异for循环语法。 Please see following program. 请参阅以下程序。

#include <iostream>
using namespace std;

int main()
{
    int num, count = 0;
    int array[]= {1,1,2,3,4,1,3,2,9,8,7};
    cout<<"Which number would you like to know about how many times it appeared?"<<endl;
    cin>>num;

    for (int i:array)
    {
        if (i == num)
            ++count;
    }

    cout<<"Number "<<num<<" appeared "<<count<<" times in the array"<<endl;
    return 0;
}

It is successfully run on GCC Linux platform. 它已在GCC Linux平台上成功运行。

Reference link Here . 参考链接在这里

So, My question is, Is it the correct way to use for loop in C++? 所以,我的问题是, 在C ++中使用for循环是否正确?

Also, Is for (int i:array) equivalent to for ( int i:array ; ; ) ? 另外, for (int i:array)等于for ( int i:array ; ; )

Sorry for my poor english. 对不起,我英语不好。 Thanks in advance. 提前致谢。

There are now (since C++11) two distinct syntaxes for for-loops, the old C-style: (自C ++ 11起)现在有两种不同的for循环语法,即旧的C风格:

for (initialization; test; increment)

and the new 和新的

for (declaration: container)

In the new syntax, the declaration declares a variable which is successively given the value of each element of the container. 在新语法中, declaration声明了一个变量,该变量被依次赋予容器中每个元素的值。 Common values of "declaration" are auto val , const auto val , auto& val , and const auto& val , depending on whether you want a reference to the value in the container or a copy, and whether you want the value constant or not. “声明”的常用值是auto valconst auto valauto& valconst auto& val ,取决于您是否要引用容器中的值或副本中的值,以及是否要常量。

Both syntaxes are correct. 两种语法都是正确的。 It rather depends what you want to do in the loop. 它取决于您要在循环中执行的操作。 My preference is to use the range based for unless I am going to need the loop counter or iterator ... in which case I fall back on the old style for. 我的偏好是使用基于范围的,除非我需要循环计数器或迭代器...在这种情况下,我将使用旧样式。

See http://en.cppreference.com/w/cpp/language/range-for for the gory details of the specification (and what is meant by "container"). 有关规范的详细内容(以及“容器”的含义),请参见http://en.cppreference.com/w/cpp/language/range-

The syntax for (int i:array) iterates through each element in the array, compared to for (int i = 0; i<sizeof(array); i++) which creates a counter that automatically increments on each iteration of the loop. for (int i = 0; i<sizeof(array); i++)相比for (int i:array)的语法会遍历for (int i:array)每个元素,后者会创建一个在循环的每次迭代时自动递增的计数器。 The counter can then be used to access elements of the array with array[i] 然后可以使用该计数器通过array[i]访问数组的元素

As for which one you'd use, it depends on what you want to do. 至于要使用哪一个,取决于您要做什么。 In your example there isn't a need to keep track of which iteration of the loop you are on, so the former will work fine. 在您的示例中,无需跟踪您正在进行的循环迭代,因此前者可以正常工作。 If you wanted to, say, print the iteration number each time then you would use the latter. 例如,如果您想每次打印迭代编号,则可以使用后者。

PS your English is perfect :) PS您的英语很完美:)

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

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