``++for++``-loops are a very powerful and versatile tool that can be used for many purposes. This flexibility comes with drawbacks:
* It is very easy to make a small mistake when writing them,
* They are relatively verbose to write,
* They do not express the intent of the code, the reader has to look at loop details to understand what the loop does.
There are algorithms that encapsulate a ``++for++``-loop and give it some meaning (``++std::all_of++``, ``++std::count_if++``, ``++std::remove_if++``...). These algorithms are well tested, efficient, and explicit and therefore should be your first choice.
This rule detects loops that go through all consecutive elements of a sequence (eg: containers, objects with begin() and end() member functions), and deal only with the current element without side-effects on the rest of the sequence.
This rule suggests using one of the supported STL algorithm patterns corresponding to your {cpp} standard when a loop matches it.
Currently, this rule supports:
* ``++std::all_of++`` (since {cpp}11) and ``++std::ranges::all_of++`` (since {cpp}20): returns ``++true++`` if all elements in the given range are matching the given predicate, ``++false++`` otherwise
* ``++std::none_of++`` (since {cpp}11) and ``++std::ranges::none_of++`` (since {cpp}20): returns ``++true++`` if no elements in the given range are matching the given predicate, ``++false++`` otherwise
* ``++std::any_of++`` (since {cpp}11) and ``++std::ranges::any_of++`` (since {cpp}20): returns ``++true++`` if at least one element in the given range is matching the given predicate, ``++false++`` otherwise
* ``++std::ranges::contains++`` (since {cpp}23): returns ``++true++`` if at least one element in the given range is equal to the given value, ``++false++`` otherwise
This rule suggests two options below when the loop doesn't match any of the supported STL algorithm patterns and you just want to iterate over all elements of a sequence:
* Range-based ``++for++``-loops, which were introduced in {cpp}11 and will run through all elements of a sequence
* ``++std::for_each++``, an algorithm that performs the same operation between two iterators (allowing more flexibility, for instance by using ``++reverse_iterator++``s, or with a variant that can loop in parallel on several elements at a time).
* {cpp} Core Guidelines - https://github.com/isocpp/CppCoreGuidelines/blob/e49158a/CppCoreGuidelines.md#es71-prefer-a-range-for-statement-to-a-for-statement-when-there-is-a-choice[ES.71: Prefer a range-`for`-statement to a `for`-statement when there is a choice]