Files
2025-10-25 03:02:53 +03:00

129 lines
4.8 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[adjacent.difference]
# 26 Algorithms library [[algorithms]](./#algorithms)
## 26.10 Generalized numeric operations [[numeric.ops]](numeric.ops#adjacent.difference)
### 26.10.12 Adjacent difference [adjacent.difference]
[🔗](#lib:adjacent_difference)
`template<class InputIterator, class OutputIterator>
constexpr OutputIterator
adjacent_difference(InputIterator first, InputIterator last,
OutputIterator result);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2>
ForwardIterator2
adjacent_difference(ExecutionPolicy&& exec,
ForwardIterator1 first, ForwardIterator1 last, ForwardIterator2 result);
template<class InputIterator, class OutputIterator, class BinaryOperation>
constexpr OutputIterator
adjacent_difference(InputIterator first, InputIterator last,
OutputIterator result, BinaryOperation binary_op);
template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2,
class BinaryOperation>
ForwardIterator2
adjacent_difference(ExecutionPolicy&& exec,
ForwardIterator1 first, ForwardIterator1 last,
ForwardIterator2 result, BinaryOperation binary_op);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13139)
Let T be the value type of decltype(first)[.](#1.sentence-1)
For the overloads that do not take an argument binary_op,
let binary_op be an lvalue
that denotes an object of type minus<>[.](#1.sentence-2)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13145)
*Mandates*:
- [(2.1)](#2.1)
For the overloads with no ExecutionPolicy, T is constructible from *first[.](#2.1.sentence-1)
acc (defined below) is
writable ([[iterator.requirements.general]](iterator.requirements.general "24.3.1General"))
to the result output iterator[.](#2.1.sentence-2)
The result of the expression binary_op(val, std::move(acc)) is writable to result[.](#2.1.sentence-3)
- [(2.2)](#2.2)
For the overloads with an ExecutionPolicy,
the result of the expressions binary_op(*first, *first) and *first are writable to result[.](#2.2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13162)
*Preconditions*:
- [(3.1)](#3.1)
For the overloads with no ExecutionPolicy, T meets the [*Cpp17MoveAssignable*](utility.arg.requirements#:Cpp17MoveAssignable "16.4.4.2Template argument requirements[utility.arg.requirements]") (Table [33](utility.arg.requirements#tab:cpp17.moveassignable "Table 33: Cpp17MoveAssignable requirements"))
requirements[.](#3.1.sentence-1)
- [(3.2)](#3.2)
For all overloads, in the ranges [first, last]
and [result, result + (last - first)], binary_op neither modifies elements
nor invalidates iterators or subranges[.](#3.2.sentence-1)[209](#footnote-209 "The use of fully closed ranges is intentional.")
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13179)
*Effects*: For the overloads with no ExecutionPolicy and a non-empty range,
the function creates an accumulator acc of type T,
initializes it with *first,
and assigns the result to *result[.](#4.sentence-1)
For every iterator i in [first + 1, last) in order,
creates an object val whose type is T,
initializes it with *i,
computes binary_op(val, std::move(acc)),
assigns the result to *(result + (i - first)), and
move assigns from val to acc[.](#4.sentence-2)
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13192)
For the overloads with an ExecutionPolicy and a non-empty range,
performs *result = *first[.](#5.sentence-1)
Then, for every d in [1, last - first - 1],
performs *(result + d) = binary_op(*(first + d), *(first + (d - 1)))[.](#5.sentence-2)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13198)
*Returns*: result + (last - first)[.](#6.sentence-1)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13202)
*Complexity*: Exactly (last - first) - 1 applications of the binary operation[.](#7.sentence-1)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L13206)
*Remarks*: For the overloads with no ExecutionPolicy,result may be equal to first[.](#8.sentence-1)
For the overloads with an ExecutionPolicy,
the ranges [first, last) and [result, result + (last - first))
shall not overlap[.](#8.sentence-2)
[209)](#footnote-209)[209)](#footnoteref-209)
The use of fully closed ranges is intentional[.](#footnote-209.sentence-1)