FREE PRACTICAL C++ GUIDE
Stop writing loops when the standard library already has a name for what you mean.
Learn how to replace common iteration patterns with clear STL algorithms, understand their C++20 ranges equivalents, and recognize when a normal loop is still the better choice.
Get the Free Guide
Make the Intent of Your Code Visible
Loops are flexible enough to express almost any operation over a sequence.
That flexibility is useful, but it also means a loop often describes how something happens rather than what the code is trying to accomplish.
A named STL algorithm can often make that intent obvious:
- find an element;
- check whether something exists;
- count matching values;
- transform data;
- filter elements;
- sort a range;
- find an extreme value;
- combine many values into one result.
The goal is not to eliminate every loop.
The goal is to recognize when the standard library already provides a clearer abstraction.
From This…
bool has_negative = false;
for (int value : values) {
if (value < 0) {
has_negative = true;
break;
}
}
…to This
const bool has_negative =
std::ranges::any_of(
values,
[](int value) {
return value < 0;
});
The second version tells the reader the question immediately:
Does any element satisfy this condition?
No temporary state. No manual early exit. No need to reconstruct the intent from control flow.

What You’ll Learn
Find the Right Element
Use find and find_if when you need the first matching element and understand when a linear search is the wrong data-structure choice.
Ask Questions About a Range
Use any_of, all_of, and none_of to express boolean questions directly and take advantage of short-circuiting behavior.
Count and Transform Data
Learn when count, count_if, and transform communicate intent better than hand-written iteration.
Filter and Remove Elements
Understand the difference between copy_if, erase_if, remove_if, and lazy filtering with std::views::filter.
Sort and Find Extremes
Use sort, stable_sort, min_element, max_element, and minmax_element without doing unnecessary work.
Reduce Many Values to One
Use std::accumulate and C++23 range folds when many elements need to become a single result.
Classic STL and Modern Ranges
The guide shows both traditional STL algorithms and their modern ranges counterparts where appropriate.
For example:
std::find(first, last, value)
becomes:
std::ranges::find(range, value)
and:
std::transform(...)
can often become:
std::ranges::transform(...)
You’ll see where C++20 ranges remove iterator boilerplate, where projections simplify code, and where the older algorithms are still perfectly appropriate.
Algorithms Covered
The guide includes practical examples and decision guidance for:
find/find_ifany_of/all_of/none_ofcount/count_iftransformcopy_ifremove_if/erase_ifviews::filtersort/stable_sortmin_element/max_element/minmax_elementaccumulate- C++23
fold_left for_each
It also includes an algorithm selection cheat sheet to help you choose an appropriate operation for the task.
A Loop Is Sometimes Still the Better Tool
Modern C++ is not a competition to see who can remove the most for keywords.
A normal loop may be clearer when:
- several unrelated operations happen during one iteration;
- complex
breakorcontinuebehavior is required; - several pieces of state evolve together;
- error handling and logging are mixed into the operation;
- expressing the logic through several algorithms or views would make it harder to follow.
The useful rule is not:
Never write loops.
It is:
Before writing a loop, check whether the standard library already has a name for the operation you are trying to express.
Write Code That Says What It Means
STL algorithms are valuable not because they make code shorter, but because they give common operations names.
Instead of reconstructing the purpose of a loop from several lines of state and control flow, the reader can often recognize the operation immediately.
Get the guide and build a practical mental model for choosing between loops, classic STL algorithms, and modern ranges.