| Constrained algorithms and algorithms on ranges(C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Constrained algorithms, e.g.ranges::copy,ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Execution policies(C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized memory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Common mathematical functions | |||||||||||||||||||||||||||||||
| Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
| Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
| Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
| Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
| Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray) | |||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||
| Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
| Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Defined in header <numeric> | ||
template<class InputIt> typenamestd::iterator_traits<InputIt>::value_type | (1) | (since C++17) (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt> typenamestd::iterator_traits<ForwardIt>::value_type | (2) | (since C++17) |
template<class InputIt,class T> T reduce( InputIt first, InputIt last, T init); | (3) | (since C++17) (constexpr since C++20) |
template<class ExecutionPolicy,class ForwardIt,class T> T reduce( ExecutionPolicy&& policy, | (4) | (since C++17) |
template<class InputIt,class T,class BinaryOp> T reduce( InputIt first, InputIt last, T init, BinaryOp op); | (5) | (since C++17) (constexpr since C++20) |
template<class ExecutionPolicy, class ForwardIt,class T,class BinaryOp> | (6) | (since C++17) |
[first, last), possibly permuted and aggregated in unspecified manner, along with the initial valueinit overop.std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> istrue. | (until C++20) |
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> istrue. | (since C++20) |
Givenbinary_op as the actual binary operation:
T, the program is ill-formed:T is notMoveConstructible.[first, last).[first, last].Contents |
| first, last | - | the pair of iterators defining therange of elements to apply the algorithm to |
| init | - | the initial value of the generalized sum |
| policy | - | theexecution policy to use |
| op | - | binaryFunctionObject that will be applied in unspecified order to the result of dereferencing the input iterators, the results of otherop andinit. |
| Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
-ForwardIt must meet the requirements ofLegacyForwardIterator. | ||
[first, last) overop.Thegeneralized sum of a group of elements over an binary operationbinary_op is defined as follows:
Given\(\scriptsize N\)N asstd::distance(first, last):
The overloads with a template parameter namedExecutionPolicy report errors as follows:
ExecutionPolicy is one of thestandard policies,std::terminate is called. For any otherExecutionPolicy, the behavior is implementation-defined.std::reduce behaves likestd::accumulate except the elements of the range may be grouped and rearranged in arbitrary order.
Side-by-side comparison betweenstd::reduce andstd::accumulate:
#if PARALLEL#include <execution>#define SEQ std::execution::seq,#define PAR std::execution::par,#else#define SEQ#define PAR#endif #include <chrono>#include <iomanip>#include <iostream>#include <locale>#include <numeric>#include <utility>#include <vector> int main(){std::cout.imbue(std::locale("en_US.UTF-8"));std::cout<<std::fixed<<std::setprecision(1); auto eval=[](auto fun){constauto t1=std::chrono::high_resolution_clock::now();constauto[name, result]= fun();constauto t2=std::chrono::high_resolution_clock::now();conststd::chrono::duration<double,std::milli> ms= t2- t1;std::cout<<std::setw(28)<<std::left<< name<<"sum: "<< result<<'\t'<<"time: "<< ms.count()<<" ms\n";}; {conststd::vector<double> v(100'000'007,0.1); eval([&v]{returnstd::pair{"std::accumulate (double)",std::accumulate(v.cbegin(), v.cend(),0.0)};}); eval([&v]{returnstd::pair{"std::reduce (seq, double)", std::reduce(SEQ v.cbegin(), v.cend())};}); eval([&v]{returnstd::pair{"std::reduce (par, double)", std::reduce(PAR v.cbegin(), v.cend())};});} {conststd::vector<long> v(100'000'007,1); eval([&v]{returnstd::pair{"std::accumulate (long)",std::accumulate(v.cbegin(), v.cend(),0l)};}); eval([&v]{returnstd::pair{"std::reduce (seq, long)", std::reduce(SEQ v.cbegin(), v.cend())};}); eval([&v]{returnstd::pair{"std::reduce (par, long)", std::reduce(PAR v.cbegin(), v.cend())};});}}
Possible output:
// POSIX: g++ -std=c++23 ./example.cpp -ltbb -O3; ./a.outstd::accumulate (double) sum: 10,000,000.7time: 356.9 msstd::reduce (seq, double) sum: 10,000,000.7time: 140.1 msstd::reduce (par, double) sum: 10,000,000.7time: 140.1 msstd::accumulate (long) sum: 100,000,007time: 46.0 msstd::reduce (seq, long) sum: 100,000,007time: 67.3 msstd::reduce (par, long) sum: 100,000,007time: 63.3 ms // POSIX: g++ -std=c++23 ./example.cpp -ltbb -O3 -DPARALLEL; ./a.outstd::accumulate (double) sum: 10,000,000.7time: 353.4 msstd::reduce (seq, double) sum: 10,000,000.7time: 140.7 msstd::reduce (par, double) sum: 10,000,000.7time: 24.7 msstd::accumulate (long) sum: 100,000,007time: 42.4 msstd::reduce (seq, long) sum: 100,000,007time: 52.0 msstd::reduce (par, long) sum: 100,000,007time: 23.1 ms
| sums up or folds a range of elements (function template)[edit] | |
| applies a function to a range of elements, storing results in a destination range (function template)[edit] | |
(C++17) | applies an invocable, then reduces out of order (function template)[edit] |
(C++23) | left-folds a range of elements (algorithm function object)[edit] |