Back to results for “Lambda Expressions”
C++ › Ranges & Views

What is a projection in range algorithms?

Advanced

Answer

A projection is an optional callable passed to a range algorithm that transforms each element before the algorithm's core logic (comparison, predicate) sees it. It lets you, for example, sort a vector of structs by a member without writing a custom comparator, by projecting to that member.
std::ranges::sort(people, {}, &Person::age); // sort by age
Real-world example Sorting a vector of Person by age uses a projection (&Person::age) instead of a comparator lambda.

Common follow-ups: What does a projection transform? | How does it simplify sorting by a member?