Foreach和For循环之间的区别? [英] Difference between Foreach and For Loops?
问题描述
如果任何一个都可以完成相同的工作,那么foreach和for循环之间的真正区别是什么?我正在学习C ++,显然它的数组没有foreach循环:(
What's the real difference between a foreach and for loop if either can get the same job done? I'm learning C++ and apparently there is no foreach loop for its arrays :(
推荐答案
C ++中没有"foreach"语言构造,至少在字面上没有.不过,C ++ 11引入了与foreach循环一样好"的东西.
There is no "foreach" language construct in C++, a least not literally. C++11 introduces something that's "as good as" a foreach loop, though.
传统的for
循环与评估条件和执行重复操作有关.这是一个非常通用的控制结构.它最流行的用途是遍历容器或数组的内容,但这只是您可以使用的一小部分.
The traditional for
loop has something to do with evaluating conditions and performing repeated operations. It's a very general control structure. Its most popular use is to iterate over container or array contents, but that's just a tiny fraction of what you can do with it.
另一方面,"foreach"循环是专门设计用来遍历容器元素的.
A "foreach" loop, on the other hand, is explicitly designed to iterate over container elements.
示例:
int arr[5] = { 1, 3, 5, 2, 4 };
for (int & n : arr) { n *= 2; } // "for-each" loop, new in C++11
for (size_t i = 0; i != 5; ++i) { arr[i] *= 2; } // "classic" for loop
在第二个中,我们使用传统的for
循环来增加辅助变量i
以便访问容器arr
.第一个基于范围的 循环不公开迭代的任何细节,而只是说对集合中的每个元素执行此操作".
In the second for, we use a traditional for
loop to increment an auxiliary variable i
in order to access the container arr
. The first, range-based loop does not expose any details of the iteration, but just says "do this and that to each element in the collection".
由于传统的for
循环是非常通用的控制结构,因此它也可以以不寻常的方式使用:
Since the traditional for
loop is a very general control structure, it can also be used in unusual ways:
std::vector<std::string> all_lines;
for (std::string line; std::cin >> line; all_lines.push_back(line))
{
std::cout << "On line " << (all_lines.size() + 1) << " you said: " << line << std::endl;
}
您可以轻松地将for(A; B; C)
重写为while循环:
You can trivially rewrite for(A; B; C)
as a while loop:
{ // scope!
A;
while (true && B)
{
{ // more scope!
/* for loop body */
}
C;
}
}
我可能会不提它,更不用说std::for_each
了,它与lambdas结合在一起是一个很好的自我遍历任意范围(而不是整个容器)的描述性方法.它从第1天开始就存在,但是在使用lambda之前,使用它是令人难以置信的痛苦.
I would probably be remiss not to mention the library function template std::for_each
from <algorithm>
, which in conjunction with lambdas is a very nice and self-descriptive way to iterate over arbitrary ranges (not just entire containers). It has existed since Day 1, but before lambdas it was a show-stopping pain to use.
更新:我想到的其他地方可能与此有关:"foreach"循环通常假定您不要修改容器.修改容器的常见循环类型需要传统的for
循环;例如在这种典型的erase
模式中:
Update: I thought of something else that might be relevant here: A "foreach" loop generally assumes that you don't modify the container. A common type of looping that modifies the container requires the traditional for
-loop; as for example in this typical erase
pattern:
for(Container::const_iterator it = v.begin(); it != v.end() /* not hoisted! */; /* no increment */ )
{
// do something
if (suitable_condition)
{
v.erase(it++); // or it = v.erase(it), depending on container type
}
else
{
++it;
}
}
这篇关于Foreach和For循环之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!