std::partial_sum
来自 cppreference.cn
定义于头文件 <numeric> |
||
template< class InputIt, class OutputIt > OutputIt partial_sum( InputIt first, InputIt last, |
(1) | (constexpr since C++20) |
template< class InputIt, class OutputIt, class BinaryOp > OutputIt partial_sum( InputIt first, InputIt last, |
(2) | (constexpr since C++20) |
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 问题 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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 242 | C++98 | op 不能有副作用 | 它不能修改所涉及的范围 |
LWG 539 | C++98 | 结果所需的类型要求 有效评估和赋值缺失 |
已添加 |
[编辑] 参见
计算范围中相邻元素之间的差值 (函数模板) | |
对元素范围求和或折叠 (函数模板) | |
(C++17) |
类似于 std::partial_sum,在第 ith 个和中包含第 ith 个输入元素 (函数模板) |
(C++17) |
类似于 std::partial_sum,从第 ith 个和中排除第 ith 个输入元素 (函数模板) |