Using std::ranges::fold_left()
over std::accumulate()
has several advantages, mainly related to flexibility, control, and modern C++Â features.
fold_left()
is part of the C++20 ranges library, which provides a more modern and flexible approach to handling collections.
It works seamlessly with range-based algorithms and can be combined with other range views and actions, allowing for more expressive and readable code.
accumulate()
only supports left-to-right folding.
fold_left()
explicitly supports left-to-right folding, and C++23 introduces fold_right()
for right-to-left folding, providing more control over the order of operations.
While accumulate()
requires an initial value, fold_left_first()
and fold_right_last()
variants use the first or last element of the range as the initial value, reducing boilerplate code and potential errors.
fold_left_first()
and fold_right_last()
return a std::optional
, making it easy to handle empty ranges gracefully without additional checks.
fold_left_with_iter()
and fold_left_first_with_iter()
return the final iterator position alongside the result.
This provides additional information about the processing, which can be useful for further operations.
Both accumulate()
and fold_left()
support custom binary operations, but the latter integrates better with range-based customization and lambdas.
Here’s an example comparing std::accumulate()
with std::ranges::fold_left()
:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> numbers{1, 2, 3, 4, 5};
// Using std::accumulate()
int accumulate_result = std::accumulate(
numbers.begin(), numbers.end(), 0);
std::cout << "Accumulate result: "
<< accumulate_result << "\n";
// Using std::ranges::fold_left()
int fold_left_result = std::ranges::fold_left(
numbers, 0, std::plus<>());
std::cout << "Fold left result: "
<< fold_left_result;
}
Accumulate result: 15
Fold left result: 15
In this example, we use a custom lambda as our folding operation:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers{1, -2, 3, -4, 5};
auto abs_sum = [](int x, int y) {
return std::abs(x) + std::abs(y); };
int result = std::ranges::fold_left(
numbers, 0, abs_sum);
std::cout << "Result: " << result;
}
Result: 15
In summary, std::ranges::fold_left()
offers more flexibility, control, and modern features compared to std::accumulate()
, making it a preferred choice for many scenarios in modern C++Â programming.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to the 6 new folding algorithms added in C++23, providing alternatives to std::reduce
and std::accumulate