Are there any advantages of std::for_each
over for
loop? To me, std::for_each
only seems to hinder the readability of code. Why do then some coding standards recommend its use?
C++ – Advantages of std::for_each over for loop
ccoding-styleforeachstl
Related Topic
- C++ – Why is “using namespace std;” considered bad practice
- C++ – Why are elementwise additions much faster in separate loops than in a combined loop
- C# – reason for C#’s reuse of the variable in a foreach
- Javascript – For-each over an array in JavaScript
- Php – How does PHP ‘foreach’ actually work
- C++ – Image Processing: Algorithm Improvement for ‘Coca-Cola Can’ Recognition
- Java – Why is processing a sorted array faster than processing an unsorted array
- C# – System.Windows.Application.GetResourceStream returns null
Best Answer
The nice thing with C++11 (previously called C++0x), is that this tiresome debate will be settled.
I mean, no one in their right mind, who wants to iterate over a whole collection, will still use this
Or this
when the range-based
for
loop syntax is available:This kind of syntax has been available in Java and C# for some time now, and actually there are way more
foreach
loops than classicalfor
loops in every recent Java or C# code I saw.