iterating through a sequence of characters using an index is out of date
|
There are at least two major advantages to using the standard algorithms over roll-your-own loops:
1. Correctness: the standard library contains code that is highly likely to be correct.
2. Recognizability: standard algorithms have known post-conditions, handle corner cases gracefully, and have known (good) asymptotic complexity.
There are other advantages (genericity, e.g.), but those are the most important. Hand-rolled loops are not assuredly correct, nor are they recognizable by name. They might introduce subtle performance issues, fail to address corner cases, and it can be tricky to guarantee their post-conditions.
Unless we're planning to deep dive into the properties of the expression
c? x: y, the conditional operator can be taught to a noob who understands
if in about five minutes + some exercises.
I am not sure if using std::for_each would be a solution. |
WRT
std::for_each specifically, it's been subsumed by range-based loops (IMO) unless we're interested in its parallel versions or are already working with iterator pairs.