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 adjacent_difference( InputIt first, InputIt last, | (1) | (constexpr since C++20) |
template<class ExecutionPolicy, class ForwardIt1,class ForwardIt2> | (2) | (since C++17) |
template<class InputIt,class OutputIt,class BinaryOp> OutputIt adjacent_difference( InputIt first, InputIt last, | (3) | (constexpr since C++20) |
template<class ExecutionPolicy, class ForwardIt1,class ForwardIt2,class BinaryOp> | (4) | (since C++17) |
LetT
be the value type ofdecltype(first).
[
first,
last)
is empty, does nothing.T
, and initializes it with*first.[
++first,
last)
in order, performs the following operations in order:T
, and initializes it with*iter.[
first,
last)
is empty, does nothing.[
1,
std::distance(first, last))
, performs the following operations in order:Givenbinary_op as the actual binary operation:
T
is not constructible from*first.
| (since C++20) |
[
first,
last)
and[
d_first,
d_last)
overlaps.[
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 |
d_first | - | the beginning of the destination range |
policy | - | theexecution policy to use |
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. | ||
-ForwardIt1, ForwardIt2 must meet the requirements ofLegacyForwardIterator. |
Iterator to the element past the last element written, ord_first if[
first,
last)
is empty.
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.adjacent_difference (1) |
---|
template<class InputIt,class OutputIt>constexpr// since C++20OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first){if(first== last)return d_first; typedeftypenamestd::iterator_traits<InputIt>::value_type value_t; value_t acc=*first;*d_first= acc; while(++first!= last){ value_t val=*first;*++d_first= val- std::move(acc);// std::move since C++20 acc= std::move(val);} return++d_first;} |
adjacent_difference (3) |
template<class InputIt,class OutputIt,class BinaryOp>constexpr// since C++20OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first, BinaryOp op){if(first== last)return d_first; typedeftypenamestd::iterator_traits<InputIt>::value_type value_t; value_t acc=*first;*d_first= acc; while(++first!= last){ value_t val=*first;*++d_first= op(val, std::move(acc));// std::move since C++20 acc= std::move(val);} return++d_first;} |
acc was introduced because of the resolution ofLWG issue 539. The reason of usingacc rather than directly calculating the differences is because the semantic of the latter is confusing if the following types mismatch:
InputIt
OutputIt
acc serves as the intermediate object to cache values of the iterated elements:
InputIt
char i_array[4]={100,100,100,100};int o_array[4]; // OK: performs conversions when needed// 1. creates “acc” of type char (the value type)// 2. “acc” is assigned to the first element of “o_array”// 3. the char arguments are used for long multiplication (char -> long)// 4. the long product is assigned to the output range (long -> int)// 5. the next value of “i_array” is assigned to “acc”// 6. go back to step 3 to process the remaining elements in the input rangestd::adjacent_difference(i_array, i_array+4, o_array,std::multiplies<long>{});
#include <array>#include <functional>#include <iostream>#include <iterator>#include <numeric>#include <vector> void println(auto comment,constauto& sequence){std::cout<< comment;for(constauto& n: sequence)std::cout<< n<<' ';std::cout<<'\n';}; int main(){// Default implementation - the difference between two adjacent itemsstd::vector v{4,6,9,13,18,19,19,15,10}; println("Initially, v = ", v); std::adjacent_difference(v.begin(), v.end(), v.begin()); println("Modified v = ", v); // Fibonaccistd::array<int,10> a{1}; std::adjacent_difference(std::begin(a),std::prev(std::end(a)),std::next(std::begin(a)),std::plus<>{}); println("Fibonacci, a = ", a);}
Output:
Initially, v = 4 6 9 13 18 19 19 15 10 Modified v = 4 2 3 4 5 1 0 -4 -5 Fibonacci, a = 1 1 2 3 5 8 13 21 34 55
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 |
LWG 3058 | C++17 | for overloads(2,4), the result of each invocation ofoperator- orop was assigned to a temporary object, and that object is assigned to the output range | assign the results to the output range directly |
computes the partial sum of a range of elements (function template)[edit] | |
sums up or folds a range of elements (function template)[edit] |