C++23 Fold Algorithms

Choosing Between Fold Left and Fold Right

How do I decide whether to use fold_left() or fold_right()?

Abstract art representing computer programming

Deciding between fold_left() and fold_right() depends on how you want to process the elements in your collection and the specific requirements of your operation.

Fold Direction

The fold_left() function processes elements from left to right, starting with the first element and moving towards the last. It's suitable when the order of combining elements from the beginning to the end is important.

fold_right() processes elements from right to left, starting with the last element and moving towards the first. It's useful when you need to combine elements from the end to the beginning.

Associativity and Commutativity

If your operation is associative and commutative, the result of fold_left() and fold_right() will be the same. For example, addition and multiplication are both associative and commutative.

If your operation is not associative or not commutative, the order in which you combine elements will affect the result. In such cases, the choice between fold_left() and fold_right() becomes critical.

Initial Value

Both fold_left() and fold_right() require an initial value. However, fold_left_first() and fold_right_last() do not require an initial value, as they use the first and last element of the range, respectively, as the initial value.

Example

Let's look at an example where the operation is not commutative:

#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> numbers{1, 2, 3, 4, 5};

  // Using fold_left() with subtraction
  int fold_left_result = std::ranges::fold_left(
    numbers, 0, std::minus<>());
  std::cout << "Fold left result: "
    << fold_left_result << "\n";

  // Using fold_right() with subtraction
  int fold_right_result = std::ranges::fold_right(
    numbers, 0, std::minus<>());
  std::cout << "Fold right result: "
    << fold_right_result << "\n";
}
Fold left result: -15
Fold right result: 3

In the example above, the results of fold_left() and fold_right() differ significantly due to the non-commutative nature of the subtraction operation.

Choosing Based on Operation Requirements

  • Use fold_left() when the operation logically combines elements from the beginning to the end, such as calculating a running total.
  • Use fold_right() when the operation logically combines elements from the end to the beginning, such as parsing or evaluating expressions in reverse.

In summary, the choice between fold_left() and fold_right() depends on the specific requirements of your operation and the nature of the elements in your collection.

Understanding how your operation interacts with the direction of folding will help you make the right decision.

This Question is from the Lesson:

C++23 Fold Algorithms

An introduction to the 6 new folding algorithms added in C++23, providing alternatives to std::reduce and std::accumulate

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

C++23 Fold Algorithms

An introduction to the 6 new folding algorithms added in C++23, providing alternatives to std::reduce and std::accumulate

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved