cpp/algorithm/adjacent difference:修订间差异
来自cppreference.com
小 →返回值 |
小 修正 constexpr |
||
| 第3行: | 第3行: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header | numeric}} | {{dcl header | numeric}} | ||
{{dcl | {{dcl | num=1 |= | ||
template< class InputIt, class OutputIt > | template< class InputIt, class OutputIt > | ||
OutputIt adjacent_difference( InputIt first, InputIt last, | OutputIt adjacent_difference( InputIt first, InputIt last, | ||
OutputIt d_first ); | OutputIt d_first ); | ||
}} | }} | ||
{{dcl | since=c++17 | until= | num=2 |1= | {{dcl | since=c++17 | until= | num=2 |1= | ||
| 第17行: | 第13行: | ||
ForwardIt2 d_first ); | ForwardIt2 d_first ); | ||
}} | }} | ||
{{dcl | {{dcl | num=3 |= | ||
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, | ||
OutputIt d_first, BinaryOperation op ); | OutputIt d_first, BinaryOperation op ); | ||
}} | }} | ||
{{dcl | since=c++17 | until= | num=4 |1= | {{dcl | since=c++17 | until= | num=4 |1= | ||
2019年6月13日 (四) 10:58的版本
| 在标头 <numeric> 定义
|
||
| |
(1) | |
| |
(2) | (C++17 起) |
| |
(3) | |
| |
(4) | (C++17 起) |
计算 [first, last) 范围中每对相邻元素的第二个和第一个的差,并写入它们到始于 d_first + 1 的范围。写入不修改的 *first 副本到 *d_first 。
1,3) 首先,创建类型为
InputIt 的 value_type 的积累器 acc ,以 *first 初始化,并赋值为 *d_first 的结果。然后,对于 [first + 1, last) 中按顺序的每个迭代器 i ,创建类型为 InputIt 的 value_type 的对象 val ,以 *i 初始化,计算 val - acc(C++20 前)val - std::move(acc)(C++20 起) (重载 (1) )或 op(val, acc)(C++20 前)op(val, std::move(acc))(C++20 起) (重载 (3) ),赋值结果到 *(d_first + (i - first)) ,并从 val 移动赋值到 acc 。first 可以等于 d_first 。2,4) 进行
*d_first = *first; 。对每个 [1, last - first - 1] 中的 d ,赋 *(first + d) - *(first + d - 1) (重载 (2) )或 op(*(first + d), *(first + d - 1)) (重载 (4) )给 *(d_first + d) 。按照 policy 执行之。此重载只有在 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 时才会参与重载决议。 若输入与输出范围以任何方式重叠则行为未定义。
等价操作:
*(d_first) = *first;
*(d_first+1) = *(first+1) - *(first);
*(d_first+2) = *(first+2) - *(first+1);
*(d_first+3) = *(first+3) - *(first+2);
...
|
|
(C++11 前) |
|
|
(C++11 起) |
参数
| first, last | - | 元素范围 |
| d_first | - | 目标范围的起始 |
| policy | - | 所用的执行策略 |
| op | - | 被使用的二元函数对象。 该函数的签名应当等价于:
签名中并不需要有 |
| 类型要求 | ||
-InputIt 必须满足老式输入迭代器 (LegacyInputIterator) 。InputIt 的 value_type 必须可移动赋值且可从 *first 的类型构造
| ||
-OutputIt 必须满足老式输出迭代器 (LegacyOutputIterator) 。acc (积累的值)和 val - acc 或 op(val, acc)(C++20 前)val - std::move(acc) 或 op(val, std::move(acc))(C++20 起) 结果都必须可写入 OutputIt
| ||
-ForwardIt1, ForwardIt2 必须满足老式向前迭代器 (LegacyForwardIterator) 。*first 、 *first - *first (对于 (2) )和 op(*first, *first) (对于 (4) )的结果必须可写入 ForwardIt2 。
| ||
返回值
指向最后被写入元素后一位置的迭代器。
注意
若 first == last ,则此函数无效果并且只会返回 d_first 。
复杂度
恰好应用 (last - first) - 1 次二元运算。
异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy是标准策略之一,那么调用 std::terminate。对于任何其他ExecutionPolicy,行为由实现定义。 - 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| 版本一 |
|---|
template<class InputIt, class OutputIt>
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++20 起有 std::move
acc = std::move(val);
}
return ++d_first;
}
|
| 版本二 |
template<class InputIt, class OutputIt, class BinaryOperation>
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++20 起有 std::move
acc = std::move(val);
}
return ++d_first;
}
|
示例
运行此代码
#include <numeric>
#include <vector>
#include <iostream>
#include <functional>
int main()
{
// 默认实现——二个项之间的差
std::vector<int> v{2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
std::adjacent_difference(v.begin(), v.end(), v.begin());
for (auto n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
// 斐波那契
// 注意,列表中下一项是当前迭代的结果
v = std::vector<int>(10);
v[0] = 1;
std::adjacent_difference(v.begin(), v.end() - 1, v.begin() + 1, std::plus<int>());
for (auto n : v) {
std::cout << n << ' ';
}
std::cout << '\n';
}
输出:
2 2 2 2 2 2 2 2 2 2
1 1 2 3 5 8 13 21 34 55
参阅
| 计算范围中元素的部分和 (函数模板) | |
| 求和或折叠范围中元素 (函数模板) |