本文共 5136 字,大约阅读时间需要 17 分钟。
c foreach循环
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循环结构。
So basically a for-each loop iterates over the elements of , , 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:
如我们所见:
type
is the data type of the variable_name 在循环初始化期间, 元素变量声明是我们需要声明将在数组或向量上迭代的变量的部分。 在这里, type
是variable_name的数据类型 Note: It is suggested to keep the data type of the variable the same as that of the array or vector. If the data type is not the same, then the elements are going to be type-casted and then stored into the variable.
注意:建议使变量的数据类型与数组或向量的数据类型相同。 如果数据类型不同,则元素将被类型转换,然后存储到变量中。
The code given below illustrates the use of the for-each loop in C++,
下面给出的代码说明了C ++中for-each循环的用法,
#includeusing namespace std; int main() { int arr[]={1,2,3,4,5}; //array initialization cout<<"The elements are: "; for(int i : arr) { cout< <<" "; } return 0;}
Output:
输出 :
The elements are: 1 2 3 4 5
Let’s break down the code and look at it line-by-line:
让我们分解代码,逐行看一下:
arr[]
is initialized with some values {1 , 2 , 3 , 4 , 5} 数组arr[]
被初始化为一些值{1、2、3、4、5} arr
is the array name which also serves as the base address of the respective array arr
是数组名称,它也用作相应数组的基地址 Please note: While declaring the variable ‘i‘ we could also use the auto
datatype instead of int
. This ensures that the type of the variable is deduced from the array type, and no data type conflicts occur.
请注意 :在声明变量' i '时,我们也可以使用auto
数据类型代替int
。 这样可以确保从数组类型推导变量的类型,并且不会发生数据类型冲突。
For example:
例如:
#includeusing namespace std; int main() { int array[]={1,4,7,4,8,4}; cout<<"The elements are: "; for(auto var : array) { cout< <<" "; } return 0;}
Output:
输出 :
The following code illustrates the use of the for-each loop for iterating over a vector
.
以下代码说明了for-each循环在vector
进行迭代的用法。
#include#include using namespace std; int main() { vector vec={11,22,33,44,55,66}; cout<<"The elements are: "; for(auto var : vec) { cout< <<" "; } return 0;}
Output:
输出 :
The for-each loop for vector works in the same way as it does for an array. Furthermore, the only differences are the vector declaration, initialization and the different operations that can be performed over it.
向量的for-each循环的工作方式与数组相同。 此外,唯一的区别是向量声明,初始化以及可以对其执行的不同操作。
The foreach loop in C++ has its own pros and cons. The code is easy to read but it restricts some of the actions that the normal for loop offers. Hence, it completely depends on the user what he/she wants the loop to perform and choose accordingly.
C ++中的foreach循环有其优点和缺点。 该代码易于阅读,但是它限制了普通for循环提供的某些操作。 因此,这完全取决于用户他/她希望循环执行什么并相应地选择。
翻译自:
c foreach循环
转载地址:http://hgozd.baihongyu.com/