在线文档教程
C++
算法 | Algorithm

std::partial_sum

STD:部分[医]相加

Defined in header
template< class InputIt, class OutputIt > OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first (1)
template< class InputIt, class OutputIt, class BinaryOperation > OutputIt partial_sum( InputIt first, InputIt last, OutputIt d_first, BinaryOperation op (2)

计算范围子范围中元素的部分和。[first, last)并将它们写入范围,从d_first第一个版本使用operator+为了总结这些元素,第二个版本使用给定的二进制函数。op...

等效操作:

二次

*(d_first) = *first; *(d_first+1) = *first + *(first+1 *(d_first+2) = *first + *(first+1) + *(first+2 *(d_first+3) = *first + *(first+1) + *(first+2) + *(first+3 ...

二次

op must not have side effects.(until C++11)
op must not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved.(since C++11)

参数

first, last-the range of elements to sum
d_first-the beginning of the destination range; may be equal to first
op-binary operation function object that will be applied. The signature of the function should be equivalent to the following: Ret fun(const Type1 &a, const Type2 &b The signature does not need to have const &. The type Type1 must be such that an object of type iterator_traits<InputIt>::value_type can be implicitly converted to Type1. The type Type2 must be such that an object of type InputIt can be dereferenced and then implicitly converted to Type2. The type Ret must be such that an object of type iterator_traits<InputIt>::value_type can be assigned a value of type Ret. ​

类型要求

-输入必须符合输入器的要求。

-输出必须符合输出器的要求。

返回值

Iterator到元素的最后一个写成的元素。

复杂性

一点儿没错(last - first) - 1二进制操作的应用。

可能的实施

第一版

*。

模板<类Inputit,类OutputIt>OutputIt部分[医]总和%28---第一项,最后一项,产出d[医]第一%29{如果%281==最后%29返回d[医]第一种;类型名称std::iterator[医]性状<InputIt>*价值[医]类型SUM=%2A第一;%2A丁[医]第一=和;而%28++第一%21=最后%29{sum=sum+%2A第一;%2A++d[医]第一=和;}返回++d[医]首先;//或,因为C++14://返回STD::部分[医]总和%281,最后,d[医]首先,std::plus<>%28%29%29;}

第二版

模板<类Inputit,类OutputIt,类二进制操作>OutputIt部分[医]总和%28---第一项,最后一项,产出d[医]首先,BinaryOperationop%29{如果%28first==最后%29返回d[医]第一种;类型名称std::iterator[医]性状<InputIt>*价值[医]类型SUM=%2A第一;%2A丁[医]第一=和;而%28++第一%21=最后%29{sum=op%28 sum,%2A第一%29;%2A++d[医]第一=和;}返回++d[医]第一;}

二次

#include <numeric> #include <vector> #include <iostream> #include <iterator> #include <functional> int main() { std::vector<int> v = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}; // or std::vector<int>v(10, 2 std::cout << "The first 10 even numbers are: "; std::partial_sum(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " ") std::cout << '\n'; std::partial_sum(v.begin(), v.end(), v.begin(), std::multiplies<int>() std::cout << "The first 10 powers of 2 are: "; for (auto 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

二次

另见

adjacent_differencecomputes the differences between adjacent elements in a range (function template)
accumulatesums up a range of elements (function template)
inclusive_scan (C++17)similar to std::partial_sum, includes the ith input element in the ith sum (function template)
exclusive_scan (C++17)similar to std::partial_sum, excludes the ith input element from the ith sum (function template)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/Algorithm/分部[医]相加