c foreach循环_C ++中的foreach循环

c foreach循环

介绍 (Introduction)

The foreach loop in C++ or more specifically, range-based for loop was introduced with the C++11. This type of for loop structure eases the traversal over an iterable data set. It does this by eliminating the initialization process and traversing over each and every element rather than an iterator. So let us dig into the respective foreach loop structure.

C ++ 11中引入了foreach循环,或更具体地说, 基于范围的for循环 。 这种类型的for循环结构简化了对可迭代数据集的遍历。 它通过消除初始化过程并遍历每个元素而不是遍历迭代器来做到这一点。 因此,让我们深入研究各自的foreach循环结构。

C ++中的foreach循环工作 (Working of the foreach loop in C++)

So basically a for-each loop iterates over the elements of arrays, vectors, or any other data sets. It assigns the value of the current element to the variable iterator declared inside the loop. Let us take a closer look at the syntax:

因此,基本上每个循环都遍历数组 , 向量或任何其他数据集的元素。 它将当前元素的值分配给循环内声明的变量迭代器。 让我们仔细看一下语法:


for(type variable_name : array/vector_name)
{
    loop statements
    ...
}

As we can see:

如我们所见:

  • During the loop initialization, the elemental variable declaration is the part where we need to de

你可能感兴趣的:(c++,java,python,数据结构,leetcode)