Fold algorithms, such as fold_left()
and fold_right()
, have many practical applications in real-world programming. These algorithms are useful for reducing collections of elements into single values using various operations. Here are some common use cases:
One of the simplest applications is summing up a collection of numbers. For instance, calculating the total cost of items in a shopping cart.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> prices{100, 200, 300, 400, 500};
int total = std::ranges::fold_left(
prices, 0, std::plus<>());
std::cout << "Total cost: " << total;
}
Total cost: 1500
Fold algorithms can be used to concatenate strings, such as creating a single sentence from a list of words.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words{
"Hello", "world", "this", "is", "C++"};
std::string sentence = std::ranges::fold_left(
words, std::string(), std::plus<>());
std::cout << "Sentence: " << sentence;
}
Sentence: HelloworldthisisC++
Fold algorithms can be used to find the maximum or minimum value in a collection.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers{3, 5, 7, 2, 8, 1};
int max_value = std::ranges::fold_left(
numbers, numbers[0],
[](int a, int b) { return std::max(a, b); }
);
std::cout << "Max value: " << max_value;
}
Max value: 8
Multiplying all elements in a collection to find the product is another common use case.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> factors{2, 3, 4, 5};
int product = std::ranges::fold_left(
factors, 1, std::multiplies<>());
std::cout << "Product: " << product;
}
Product: 120
Folding can be used to aggregate data into complex structures. For example, aggregating scores into a custom Result
 type.
#include <algorithm>
#include <iostream>
#include <vector>
struct Result {
int sum;
int count;
Result operator+(int score) const {
return Result{sum + score, count + 1}; }
void log() const {
std::cout << "Sum: " << sum
<< ", Count: " << count << "\n";
}
};
int main() {
std::vector<int> scores{10, 20, 30, 40, 50};
Result result = std::ranges::fold_left(
scores, Result{0, 0}, std::plus<>());
result.log();
}
Sum: 150, Count: 5
You can use custom operations tailored to specific needs, such as combining objects or accumulating specific properties.
#include <algorithm>
#include <iostream>
#include <vector>
struct Player {
std::string name;
int score;
};
int main() {
std::vector<Player> players{
{"Alice", 10}, {"Bob", 20}, {"Charlie", 30}
};
int total_score = std::ranges::fold_left(
players,
0,
[](int total, const Player& p) {
return total + p.score;
}
);
std::cout << "Total score: " << total_score;
}
Total score: 60
In summary, fold algorithms are versatile tools in programming, useful for summing values, concatenating strings, finding extremes, calculating products, aggregating complex data, and performing custom operations.
Their flexibility and power make them essential for various real-world applications.
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