OutputIt adjacent_difference( InputIt first, InputIt last,
class ForwardIt1, class ForwardIt2 >
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
OutputIt adjacent_difference( InputIt first, InputIt last,
class ForwardIt1, class ForwardIt2, class BinaryOp >
ForwardIt2 adjacent_difference( ExecutionPolicy&& policy,
ForwardIt1 first, ForwardIt1 last,
Let T
be the value type of decltype(first).
1) If [
first,
last)
is empty, does nothing.
Otherwise, performs the following operations in order:
T
, and initializes it with *first.[
++first,
last)
in order, performs the following operations in order:a) Creates an object val of type T
, and initializes it with *iter.
b) Computes val - acc(until C++20)val - std::move(acc)(since C++20).
c) Assigns the result to *++d_first.
d) Copy(until C++20)Move(since C++20) assigns from val to acc.
2) If [
first,
last)
is empty, does nothing.
Otherwise, performs the following operations in order:
[
1,
std::distance(first, last))
, performs the following operations in order:a) Computes curr - prev, where curr is the next ith iterator of first, and prev is the next i - 1th iterator of first.
b) Assigns the result to *dest, where dest is the next ith iterator of d_first.
3) Same as (1), but computes op(val, acc)(until C++20)op(val, std::move(acc))(since C++20) instead.
4) Same as (2), but computes op(curr, prev) instead.
Given binary_op as the actual binary operation:
T
is not constructible from *first.[
first,
last)
and [
d_first,
d_last)
overlaps.[
first,
last)
or [
d_first,
d_last)
.[
first,
last]
or [
d_first,
d_last]
.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 have const &.
The types Type1 and Type2 must be such that an object of type iterator_traits<InputIt>::value_type can be implicitly converted to both of them. The type Ret must be such that an object of type OutputIt can be dereferenced and assigned a value of type Ret. â
InputIt
must meet the requirements of LegacyInputIterator. -OutputIt
must meet the requirements of LegacyOutputIterator. -ForwardIt1, ForwardIt2
must meet the requirements of LegacyForwardIterator. [edit] Return value
Iterator to the element past the last element written, or d_first if [
first,
last)
is empty.
Given \(\scriptsize N\)N as std::distance(first, last):
1,2) Exactly \(\scriptsize N-1\)N-1 applications of operator-.
3,4) Exactly \(\scriptsize N-1\)N-1 applications of the binary function op.
[edit] ExceptionsThe overloads with a template parameter named ExecutionPolicy
report errors as follows:
ExecutionPolicy
is one of the standard policies, std::terminate is called. For any other ExecutionPolicy
, the behavior is implementation-defined.template<class InputIt, class OutputIt> constexpr // since C++20 OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first) { if (first == last) return d_first; typedef typename std::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++20 OutputIt adjacent_difference(InputIt first, InputIt last, OutputIt d_first, BinaryOp op) { if (first == last) return d_first; typedef typename std::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; }[edit] Notes
acc was introduced because of the resolution of LWG issue 539. The reason of using acc 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 range std::adjacent_difference(i_array, i_array + 4, o_array, std::multiplies<long>{});[edit] Example
#include <array> #include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> void println(auto comment, const auto& sequence) { std::cout << comment; for (const auto& n : sequence) std::cout << n << ' '; std::cout << '\n'; }; int main() { // Default implementation - the difference between two adjacent items std::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); // Fibonacci std::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[edit] Defect reports
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 modifyRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4