std::reduce()
and std::accumulate()
serve similar purposes, but std::reduce()
is often preferred in scenarios where performance and multi-threading are important.
std::reduce()
is designed for parallel execution, making it suitable for tasks that can benefit from multi-core processors. For example, processing large datasets such as summing elements of a big array:
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> largeNumbers(1000000, 1);
int result = std::reduce(
largeNumbers.begin(), largeNumbers.end(), 0);
std::cout << "Result: " << result;
}
Result: 1000000
In this case, std::reduce()
can utilize multiple threads to speed up the reduction process, whereas std::accumulate()
would process elements sequentially.
If the operation used is commutative and associative, std::reduce()
can be more efficient because it does not need to maintain a strict left-to-right order. This flexibility allows for optimization in how elements are combined:
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> numbers{1, 2, 3, 4, 5};
int result = std::reduce(
numbers.begin(), numbers.end(), 0);
std::cout << "Result: " << result;
}
Result: 15
For large datasets, this can lead to better performance compared to std::accumulate()
.
In cases where you need to aggregate data in a more complex manner, such as combining objects with custom logic, std::reduce()
can be more appropriate. Here's an example with a custom struct:
#include <iostream>
#include <numeric>
#include <vector>
struct Transaction {
int amount;
int count;
};
Transaction combine(
const Transaction& a, const Transaction& b) {
return {a.amount + b.amount, a.count + b.count};
}
int main() {
std::vector<Transaction> transactions{
{100, 1}, {200, 1}, {300, 1}};
Transaction result = std::reduce(
transactions.begin(), transactions.end(),
Transaction{0, 0}, combine
);
std::cout << "Total Amount: " << result.amount;
std::cout << "\nTotal Count: " << result.count;
}
Total Amount: 600
Total Count: 3
Use std::reduce()
 when:
std::accumulate()
is simpler and guarantees deterministic results, but std::reduce()
can offer significant performance benefits in the right scenarios.
Answers to questions are automatically generated and may not have been reviewed.
A detailed guide to generating a single object from collections using the std::reduce()
and std::accumulate()
algorithms