| 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,class OutputIt> OutputIt partial_sum( InputIt first, InputIt last, | (1) | (constexpr since C++20) |
template<class InputIt,class OutputIt,class BinaryOp> OutputIt partial_sum( InputIt first, InputIt last, | (2) | (constexpr since C++20) |
[first, last) is empty, does nothing.InputIt, and initializes it with*first.[1, std::distance(first, last)), performs the following operations in order:Givenbinary_op as the actual binary operation:
InputIt is not constructible from*first.InputIt.[first, last) or[d_first, d_last).[first, last] or[d_first, d_last].Contents |
| first, last | - | the pair of iterators defining therange of elements to sum |
| d_first | - | the beginning of the destination range; may be equal tofirst |
| op | - | binary operation function object that will be applied. The signature of the function should be equivalent to the following: Ret fun(const Type1&a,const Type2&b); The signature does not need to haveconst&. |
| Type requirements | ||
-InputIt must meet the requirements ofLegacyInputIterator. | ||
-OutputIt must meet the requirements ofLegacyOutputIterator. | ||
Iterator to the element past the last element written, ord_first if[first, last) is empty.
Given\(\scriptsize N\)N asstd::distance(first, last):
| partial_sum (1) |
|---|
template<class InputIt,class OutputIt>constexpr// since C++20OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first){if(first== last)return d_first; typenamestd::iterator_traits<InputIt>::value_type sum=*first;*d_first= sum; while(++first!= last){ sum= std::move(sum)+*first;// std::move since C++20*++d_first= sum;} return++d_first; // or, since C++14:// return std::partial_sum(first, last, d_first, std::plus<>());} |
| partial_sum (2) |
template<class InputIt,class OutputIt,class BinaryOp>constexpr// since C++20OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first, BinaryOp op){if(first== last)return d_first; typenamestd::iterator_traits<InputIt>::value_type acc=*first;*d_first= acc; while(++first!= last){ acc= op(std::move(acc),*first);// std::move since C++20*++d_first= acc;} return++d_first;} |
acc was introduced because of the resolution ofLWG issue 539. The reason of usingacc rather than directly summing up the results (i.e.*(d_first+2)=(*first+*(first+1))+*(first+2);) is because the semantic of the latter is confusing if the following types mismatch:
InputItOutputItacc serves as the intermediate object to store and provide the values for each step of the computation:
InputItenum not_int{ x=1, y=2}; char i_array[4]={100,100,100,100};not_int e_array[4]={x, x, y, y};int o_array[4]; // OK: uses operator+(char, char) and assigns char values to int arraystd::partial_sum(i_array, i_array+4, o_array); // Error: cannot assign not_int values to int arraystd::partial_sum(e_array, e_array+4, o_array); // OK: performs conversions when needed// 1. creates “acc” of type char (the value type)// 2. the char arguments are used for long multiplication (char -> long)// 3. the long product is assigned to “acc” (long -> char)// 4. “acc” is assigned to an element of “o_array” (char -> int)// 5. go back to step 2 to process the remaining elements in the input rangestd::partial_sum(i_array, i_array+4, o_array,std::multiplies<long>{});
#include <functional>#include <iostream>#include <iterator>#include <numeric>#include <vector> int main(){std::vector<int> v(10,2);// v = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2} std::cout<<"The first "<< v.size()<<" even numbers are: ";// write the result to the cout stream std::partial_sum(v.cbegin(), v.cend(),std::ostream_iterator<int>(std::cout," "));std::cout<<'\n'; // write the result back to the vector v std::partial_sum(v.cbegin(), v.cend(), v.begin(),std::multiplies<int>()); std::cout<<"The first "<< v.size()<<" powers of 2 are: ";for(int n: v)std::cout<< n<<' ';std::cout<<'\n';}
Output:
The first 10 even numbers are: 2 4 6 8 10 12 14 16 18 20 The first 10 powers of 2 are: 2 4 8 16 32 64 128 256 512 1024
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 242 | C++98 | op could not have side effects | it cannot modify the ranges involved |
| LWG 539 | C++98 | the type requirements needed for the result evaluations and assignments to be valid were missing | added |
| computes the differences between adjacent elements in a range (function template)[edit] | |
| sums up or folds a range of elements (function template)[edit] | |
(C++17) | similar tostd::partial_sum, includes theith input element in theith sum (function template)[edit] |
(C++17) | similar tostd::partial_sum, excludes theith input element from theith sum (function template)[edit] |