Search⌘ K

The New Algorithms

Discover how to apply C++17's new parallel algorithms including for_each_n, exclusive_scan, inclusive_scan, transform_exclusive_scan, and transform_reduce. Learn their behavior, use cases, and how they support functional programming and parallel processing with practical examples on vectors of integers and strings.

We'll cover the following...

The new algorithms are in the std namespace. std::for_each and std::for_each_n require the header <algorithm>, but the remaining 6 algorithms require the header <numeric>.

Here is an overview of the new algorithms:

Algorithm Description
std::for_each Applies a unary callable to the range.
std::for_each_n Applies a unary callable to the first n elements of the range.
std::exclusive_scan Applies from the left a binary callable up to the ith (exclusive) element of the range. The left argument of the callable is the previous result. Stores intermediate results.
std::inclusive_scan Applies from the left a binary callable up to the ith (inclusive) element of the range. The left argument of the callable is the previous result. Stores intermediate results.
std::transform_exclusive_scan First applies a unary callable to the range and
...