cpp/algorithm/adjacent difference:修订间差异
无编辑摘要 |
小无编辑摘要 |
||
| 第3行: | 第3行: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header|numeric}} | {{dcl header|numeric}} | ||
{{dcl rev multi|num=1 | {{dcl rev multi|num=1|until1=c++20|dcl1= | ||
|until1=c++20|dcl1= | |||
template< class InputIt, class OutputIt > | template< class InputIt, class OutputIt > | ||
OutputIt adjacent_difference( InputIt first, InputIt last, OutputIt d_first ); | OutputIt adjacent_difference( InputIt first, InputIt last, OutputIt d_first ); | ||
| 第18行: | 第17行: | ||
ForwardIt2 d_first ); | ForwardIt2 d_first ); | ||
}} | }} | ||
{{dcl rev multi|num=3 | {{dcl rev multi|num=3|until1=c++20| dcl1= | ||
|until1=c++20| dcl1= | |||
template< class InputIt, class OutputIt, class BinaryOperation > | template< class InputIt, class OutputIt, class BinaryOperation > | ||
OutputIt adjacent_difference( InputIt first, InputIt last, | OutputIt adjacent_difference( InputIt first, InputIt last, | ||
| 第26行: | 第24行: | ||
template< class InputIt, class OutputIt, class BinaryOperation > | template< class InputIt, class OutputIt, class BinaryOperation > | ||
constexpr OutputIt adjacent_difference( InputIt first, InputIt last, | constexpr OutputIt adjacent_difference( InputIt first, InputIt last, | ||
OutputIt d_first, BinaryOperation op ); | OutputIt d_first, | ||
BinaryOperation op ); | |||
}} | }} | ||
{{dcl|since=c++17|num=4| | {{dcl|since=c++17|num=4| | ||
| 第37行: | 第36行: | ||
{{dcl end}} | {{dcl end}} | ||
{{range|first|last}} 每对相邻元素的第二个和第一个的差,并将写入始于 {{c|d_first + 1}} 的范围。写入未经修改的 {{c|*first}} 副本到 {{c|*d_first}}。重载 {{v|1,2}} 使用 {{c/core|operator-}} 计算差值,重载 {{v|3,4}} 使用{{c|op}}{{rev inl|since=c++11|,两个重载都会将 {{ltt std|cpp/utility/move}} 应用到它们的数}}。 | |||
重载 {{v|1}}(在 {{range|first|last}} 非空时)的等价操作,使用累加器 {{c|acc}} 存储要被减去的值: | 重载 {{v|1}}(在 {{range|first|last}} 非空时)的等价操作,使用累加器 {{c|acc}} 存储要被减去的值: | ||
| 第71行: | 第70行: | ||
... | ... | ||
}} | }} | ||
===参数=== | ===参数=== | ||
| 第80行: | 第83行: | ||
{{par hreq}} | {{par hreq}} | ||
{{par req named|InputIt|InputIterator|notes=对于重载 {{v|1,3}},它的[[cpp/iterator#类型与可写性|值类型]]必须能够从 {{c|*first}} 构造}} | {{par req named|InputIt|InputIterator|notes=对于重载 {{v|1,3}},它的[[cpp/iterator#类型与可写性|值类型]]必须能够从 {{c|*first}} 构造}} | ||
{{par req named|OutputIt|OutputIterator|notes={{c|acc}}(在上文定义)和 {{rev inl|until=c++20|{{c|val - acc}}(重载 {{v|1}})或 {{c|op(val, acc)}}(重载 {{v|3}})}}{{rev inl|since=c++20|{{c|val - std::move(acc)}}(重载 {{v|1}})或 {{c|op(val, std::move(acc))}}(重载 {{v|3}})}} | {{par req named|OutputIt|OutputIterator|notes={{c|acc}}(在上文定义)和 {{rev inl|until=c++20|{{c|val - acc}}(重载 {{v|1}})或 {{c|op(val, acc)}}(重载 {{v|3}})}}{{rev inl|since=c++20|{{c|val - std::move(acc)}}(重载 {{v|1}})或 {{c|op(val, std::move(acc))}}(重载 {{v|3}})}}结果都必须[[cpp/iterator#类型与可写性|可写入]] {{|}}}} | ||
{{par req named|ForwardIt1, ForwardIt2|ForwardIterator|notes= | {{par req named|ForwardIt1, ForwardIt2|ForwardIterator|notes= | ||
{{c|*first}} 的结果和 {{c|*first - *first}}(重载 {{v|2}})或 {{c|op(*first, *first)}}(重载 {{v|4}})的结果必须可[[cpp/iterator#类型与可写性|可写入]] {{ | {{c|*first}} 的结果和 {{c|*first - *first}}(重载 {{v|2}})或 {{c|op(*first, *first)}}(重载 {{v|4}})的结果必须可[[cpp/iterator#类型与可写性|可写入]] {{|}}。}} | ||
{{par end}} | {{par end}} | ||
| 第90行: | 第93行: | ||
===复杂度=== | ===复杂度=== | ||
给定 {{tt|N}} 为 {{c|std::distance(first, last) - 1}}: | 给定 {{tt|N}} 为 {{c|std::distance(first, last) - 1}}: | ||
@1,2@ 应用 {{tt|N}} 次 {{c/core|operator-}} | @1,2@ 应用 {{tt|N}} 次 {{c/core|operator-}} | ||
@3,4@ 应用 {{tt|N}} 次二元函数 {{c|op}} | @3,4@ 应用 {{tt|N}} 次二元函数 {{c|op}} | ||
===异常=== | ===异常=== | ||
{{cpp/algorithm/parallel exceptions reporting behavior}} | {{cpp/algorithm/parallel exceptions reporting behavior}} | ||
===可能的实现=== | ===可能的实现=== | ||
{{eq impl | {{eq impl | ||
|1= | |1= | ||
template<class InputIt, class OutputIt> | template<class InputIt, class OutputIt> | ||
constexpr // C++20 起 | constexpr // C++20 起 | ||
| 第119行: | 第122行: | ||
return ++d_first; | return ++d_first; | ||
} | } | ||
|title2= | |title2=|2= | ||
template<class InputIt, class OutputIt, class BinaryOperation> | template<class InputIt, class OutputIt, class BinaryOperation> | ||
constexpr // C++20 起 | constexpr // C++20 起 | ||
| 第158行: | 第161行: | ||
char i_array[4] = {100, 100, 100, 100}; | char i_array[4] = {100, 100, 100, 100}; | ||
int o_array[4]; | int o_array[4]; | ||
// OK:在需要时进行转换 | // OK:在需要时进行转换 | ||
// 1. 创建 char 类型 | // 1. 创建 char 类型(值类型) | ||
// 2. 将 acc 赋给 o_array 的首个元素 | // 2. 将 acc 赋给 o_array 的首个元素 | ||
// 3. 将 char 实参用于 long 乘法(char -> long) | // 3. 将 char 实参用于 long 乘法(char -> long) | ||
// 4. 将 long 积赋到 o_array 中(long -> int) | // 4. 将 long 积赋到 o_array 中(long -> int) | ||
// 5. 将 i_array 的下个值赋给 acc | // 5. 将 i_array 的下个值赋给 acc | ||
// 6. 回到第 3 步,处理输入序列的剩余元素 | // 6. 回到第 3 步,处理输入序列的剩余元素 | ||
| 第171行: | 第175行: | ||
{{example | {{example | ||
|code= | |code= | ||
#include <array> | #include <array> | ||
#include <iostream> | #include <iostream> | ||
#include <iterator> | #include <iterator> | ||
auto print = [](auto comment, auto const& sequence) | auto print = [](auto comment, auto const& sequence) | ||
{ | { | ||
| 第188行: | 第192行: | ||
int main() | int main() | ||
{ | { | ||
// 默认实现——两个项之间的差 | // 默认实现——两个项之间的差 | ||
std::vector v {4, 6, 9, 13, 18, 19, 19, 15, 10}; | std::vector v {4, 6, 9, 13, 18, 19, 19, 15, 10}; | ||
print("一开始,v = ", v); | print("一开始,v = ", v); | ||
2023年11月12日 (日) 13:15的版本
| 在标头 <numeric> 定义
|
||
| (1) | (C++20 前) | |
| (C++20 起) | ||
| |
(2) | (C++17 起) |
| (3) | (C++20 前) | |
| (C++20 起) | ||
| |
(4) | (C++17 起) |
若 [first, last) 非空,则计算其每对相邻元素的第二个和第一个的差,并将各差值写入始于 d_first + 1 的范围。写入未经修改的 *first 副本到 *d_first。重载 (1,2) 使用 operator- 计算差值,重载 (3,4) 则使用给定的二元函数 op,两个重载都会将 std::move 应用到它们的右操作数(C++11 起)。
重载 (1)(在 [first, last) 非空时)的等价操作,使用累加器 acc 存储要被减去的值:
std::iterator_traits<InputIt>::value_type acc = *first;
*d_first = acc;
std::iterator_traits<InputIt>::value_type val1 = *(first + 1);
*(d_first + 1) = val1 - std::move(acc);
// 对重载 (2) 是 *(d_first + 1) = op(val1, std::move(acc));
acc = std::move(val1);
std::iterator_traits<InputIt>::value_type val2 = *(first + 2);
*(d_first + 2) = val2 - std::move(acc);
acc = std::move(val2);
std::iterator_traits<InputIt>::value_type val3 = *(first + 3);
*(d_first + 3) = val3 - std::move(acc);
acc = std::move(val3);
// ...
重载 (3)(在 [first, last) 非空时)的等价操作:
// 先执行
*d_first = *first;
// 在初始赋值后执行,不一定按顺序
*(d_first + 1) = *(first + 1) - *(first);
// 对重载 (4) 是 *(d_first + 1) = op(*(first + 1), *(first));
*(d_first + 2) = *(first + 2) - *(first + 1);
*(d_first + 3) = *(first + 3) - *(first + 2);
...
若 op 使任何迭代器失效(包括任何末尾迭代器)或者修改了所涉及范围的任何成员,则行为未定义。
对于重载 (1,3),若 std::iterator_traits<InputIt>::value_type 并非 可复制赋值 (CopyAssignable) (C++11 前)可移动赋值 (MoveAssignable) (C++11 起),则行为未定义。
参数
| first, last | - | 元素范围 |
| d_first | - | 目标范围的起始 |
| policy | - | 所用的执行策略 |
| op | - | 被使用的二元函数对象。 该函数的签名应当等价于:
签名中并不需要有 |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。对于重载 (1,3),它的值类型必须能够从 *first 构造
| ||
-OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。acc(在上文定义)和 val - acc(重载 (1))或 op(val, acc)(重载 (3))(C++20 前)val - std::move(acc)(重载 (1))或 op(val, std::move(acc))(重载 (3))(C++20 起)的结果都必须可写入 d_first
| ||
-ForwardIt1, ForwardIt2 必须满足老式向前迭代器 (LegacyForwardIterator) 。*first 的结果和 *first - *first(重载 (2))或 op(*first, *first)(重载 (4))的结果必须可可写入 d_first。
| ||
返回值
指向最后被写入元素后一位置的迭代器,或者在 [first, last) 为空时返回 d_first。
复杂度
给定 N 为 std::distance(first, last) - 1:
N 次 operator-N 次二元函数 op异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| adjacent_difference (1) |
|---|
template<class InputIt, class OutputIt>
constexpr // 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); // C++11 起有 std::move
acc = std::move(val);
}
return ++d_first;
}
|
| adjacent_difference (3) |
template<class InputIt, class OutputIt, class BinaryOperation>
constexpr // C++20 起
OutputIt adjacent_difference(InputIt first, InputIt last,
OutputIt d_first, BinaryOperation 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)); // C++11 起有 std::move
acc = std::move(val);
}
return ++d_first;
}
|
注解
acc 通过 LWG 问题 539 的解决方案引入。使用 acc 而不是直接将结果相减的原因是后者的语义在以下类型不匹配时难以理解:
InputIt的值类型OutputIt的可写入类型operator-或op的形参类型operator-或op的返回类型
acc 用来作为缓存遍历过的值的中间对象:
- 它的类型是
InputIt的值类型 - 写入
d_first的值(即operator+或op的返回值)会赋给它 - 它的值会传递给
operator+或op
char i_array[4] = {100, 100, 100, 100};
int o_array[4];
// OK:在需要时进行转换
// 1. 创建 char 类型(值类型)的 acc
// 2. 将 acc 赋给 o_array 的首个元素
// 3. 将 char 实参用于 long 乘法(char -> long)
// 4. 将 long 类型的积赋到 o_array 中(long -> int)
// 5. 将 i_array 的下个值赋给 acc
// 6. 回到第 3 步,处理输入序列的剩余元素
std::adjacent_difference(i_array, i_array + 4, o_array, std::multiplies<long>{});
示例
#include <array>
#include <functional>
#include <iostream>
#include <iterator>
#include <numeric>
#include <vector>
auto print = [](auto comment, auto const& sequence)
{
std::cout << comment;
for (const auto& n : sequence)
std::cout << n << ' ';
std::cout << '\n';
};
int main()
{
// 默认实现——两个相邻项之间的差
std::vector v {4, 6, 9, 13, 18, 19, 19, 15, 10};
print("一开始,v = ", v);
std::adjacent_difference(v.begin(), v.end(), v.begin());
print("修改后,v = ", v);
// 斐波那契
std::array<int, 10> a {1};
adjacent_difference(begin(a), std::prev(end(a)), std::next(begin(a)), std::plus<>{});
print("斐波那契,a = ", a);
}
输出:
一开始,v = 4 6 9 13 18 19 19 15 10
修改后,v = 4 2 3 4 5 1 0 -4 -5
斐波那契,a = 1 1 2 3 5 8 13 21 34 55
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 242 | C++98 | op 不能有任何副作用
|
它不能修改涉及到的范围 |
| LWG 539 | C++98 | 缺失了需要保证对结果的求值和赋值合法的条件要求 | 已补充 |
| LWG 2055 (P0616R0) |
C++11 | acc 在累加时不会被移动
|
它会被移动 |
| LWG 3058 | C++17 | 对于重载 (2,4),每次调用 operator- 或 op 都会创建临时对象存储结果,然后将该对象赋到输出范围中 |
直接将结果赋到输出范围中 |
参阅
| 计算范围中元素的部分和 (函数模板) | |
| 求和或折叠范围中元素 (函数模板) |