The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <print>
#include <vector>
#include <functional>
#include <algorithm>
int main()
{
std::vector coll{100, 2, 3, 4};
std::println("coll: {}", coll);
// compute sum of all elements:
auto sum = std::ranges::fold_left(coll, 0, std::plus<>{});
std::println("sum: {}", sum);
// compute product of all elements:
auto prod = std::ranges::fold_left(coll, 1, std::multiplies<>{});
std::println("product: {}", prod);
}