std::partial_sum
来自 cppreference.cn
定义于头文件 <numeric> |
||
template< class InputIt, class OutputIt > OutputIt partial_sum( InputIt first, InputIt last, |
(1) | (C++20 起为 constexpr) |
template< class InputIt, class OutputIt, class BinaryOp > OutputIt partial_sum( InputIt first, InputIt last, |
(2) | (C++20 起为 constexpr) |
1) 如果
[
first,
last)
为空,则不执行任何操作。 否则,按顺序执行以下操作:
- 创建一个累加器 acc,其类型为
InputIt
的值类型,并用 *first 初始化它。 - 将 acc 赋值给 *d_first。
- 对于
[
1,
std::distance(first, last))
中的每个整数 i,按顺序执行以下操作:
a) 计算 acc + *iter(C++20 前)std::move(acc) + *iter(C++20 起),其中 iter 是 first 的第 i 个迭代器。
b) 将结果赋值给 acc。
2) 同 (1),但计算 op(acc, *iter)(C++20 前)op(std::move(acc), *iter)(C++20 起) 代替。
给定 binary_op 作为实际的二元操作,
- 如果满足以下任何条件,程序将不正确:
InputIt
的值类型不可由 *first 构造。- acc 不可写入 d_first。
- binary_op(acc, *iter)(C++20 前)binary_op(std::move(acc), *iter)(C++20 起) 的结果不可隐式转换为
InputIt
的值类型。
- 给定 d_last 为将要返回的迭代器,如果满足以下任何条件,则行为未定义:
- binary_op 修改了
[
first,
last)
或[
d_first,
d_last)
中的任何元素。 - binary_op 使
[
first,
last]
或[
d_first,
d_last]
中的任何迭代器或子范围无效。
- binary_op 修改了
- ↑ 实际赋值的值是上一步中赋值的结果。我们在此假设赋值结果为 acc。
目录 |
[编辑] 参数
first, last | - | 定义要求和元素的范围的迭代器对 |
d_first | - | 目标范围的起始;可以等于 first |
op | - | 将应用的二元操作函数对象。 函数的签名应等效于以下内容 Ret fun(const Type1 &a, const Type2 &b); 签名不需要有 const &。 |
类型要求 | ||
-InputIt 必须满足 LegacyInputIterator 的要求。 | ||
-OutputIt 必须满足 LegacyOutputIterator 的要求。 |
[编辑] 返回值
指向写入的最后一个元素之后元素的迭代器,如果 [
first,
last)
为空,则为 d_first。
[编辑] 复杂度
给定 N 为 std::distance(first, last)
1) 精确执行 N-1 次 operator+ 应用。
2) 精确执行 N-1 次二元函数 op 应用。
[编辑] 可能的实现
partial_sum (1) |
---|
template<class InputIt, class OutputIt> constexpr // since C++20 OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first) { if (first == last) return d_first; typename std::iterator_traits<InputIt>::value_type sum = *first; *d_first = sum; while (++first != last) { sum = std::move(sum) + *first; // std::move since C++20 *++d_first = sum; } return ++d_first; // or, since C++14: // return std::partial_sum(first, last, d_first, std::plus<>()); } |
partial_sum (2) |
template<class InputIt, class OutputIt, class BinaryOp> constexpr // since C++20 OutputIt partial_sum(InputIt first, InputIt last, OutputIt d_first, BinaryOp op) { if (first == last) return d_first; typename std::iterator_traits<InputIt>::value_type acc = *first; *d_first = acc; while (++first != last) { acc = op(std::move(acc), *first); // std::move since C++20 *++d_first = acc; } return ++d_first; } |
[编辑] 注意
引入 acc 是因为 LWG issue 539 的解决方案。使用 acc 而不是直接求和结果(即 *(d_first + 2) = (*first + *(first + 1)) + *(first + 2);)的原因是,如果以下类型不匹配,后者的语义会令人困惑:
InputIt
的值类型OutputIt
的可写类型- operator+ 或 op 参数的类型
- operator+ 或 op 的返回类型
acc 作为中间对象,用于存储并为计算的每个步骤提供值:
- 其类型是
InputIt
的值类型 - 它被写入到 d_first
- 其值传递给 operator+ 或 op
- 它存储 operator+ 或 op 的返回值
enum not_int { x = 1, y = 2 }; char i_array[4] = {100, 100, 100, 100}; not_int e_array[4] = {x, x, y, y}; int o_array[4]; // OK: uses operator+(char, char) and assigns char values to int array std::partial_sum(i_array, i_array + 4, o_array); // Error: cannot assign not_int values to int array std::partial_sum(e_array, e_array + 4, o_array); // OK: performs conversions when needed // 1. creates “acc” of type char (the value type) // 2. the char arguments are used for long multiplication (char -> long) // 3. the long product is assigned to “acc” (long -> char) // 4. “acc” is assigned to an element of “o_array” (char -> int) // 5. go back to step 2 to process the remaining elements in the input range std::partial_sum(i_array, i_array + 4, o_array, std::multiplies<long>{});
[编辑] 示例
运行此代码
#include <functional> #include <iostream> #include <iterator> #include <numeric> #include <vector> int main() { std::vector<int> v(10, 2); // v = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2} std::cout << "The first " << v.size() << " even numbers are: "; // write the result to the cout stream std::partial_sum(v.cbegin(), v.cend(), std::ostream_iterator<int>(std::cout, " ")); std::cout << '\n'; // write the result back to the vector v std::partial_sum(v.cbegin(), v.cend(), v.begin(), std::multiplies<int>()); std::cout << "The first " << v.size() << " powers of 2 are: "; for (int n : v) std::cout << n << ' '; std::cout << '\n'; }
输出
The first 10 even numbers are: 2 4 6 8 10 12 14 16 18 20 The first 10 powers of 2 are: 2 4 8 16 32 64 128 256 512 1024
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 242 | C++98 | op 不得有副作用 | 它不能修改涉及的范围 |
LWG 539 | C++98 | 结果所需的类型要求 缺少评估和赋值的有效性 |
已添加 |
[编辑] 参阅
计算一个范围内相邻元素之间的差值 (函数模板) | |
对一个范围的元素进行求和或折叠 (函数模板) | |
(C++17) |
类似于 std::partial_sum,将第 i 个输入元素包含在第 i 个和中 (函数模板) |
(C++17) |
类似于 std::partial_sum,将第 i 个输入元素从第 i 个和中排除 (函数模板) |