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 :(
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.
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.
A "foreach" loop, on the other hand, is explicitly designed to iterate over container elements.
Example:
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
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".
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;
}
You can trivially rewrite for(A; B; C)
as a while loop:
{ // scope!
A;
while (true && B)
{
{ // more scope!
/* for loop body */
}
C;
}
}
Edit: 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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With