STL-------------partial_sum
STL http://www.cplusplus.com/reference/algorithm/count/
template <class InputIterator, class OutputIterator>
OutputIterator partial_sum ( InputIterator first, InputIterator last,
OutputIterator result );
template <class InputIterator, class OutputIterator, class BinaryOperation>
OutputIterator partial_sum ( InputIterator first, InputIterator last,
OutputIterator result, BinaryOperation binary_op );
Compute partial sums of range
Assigns to every element in the range starting at
result the partial sum of the corresponding elements in the range [first,last).
If
x represents an element in [first,last) and
y represents an element in
result, the
ys can be calculated as:
y
0 = x
0
y
1 = x
0 + x
1
y
2 = x
0 + x
1 + x
2
y
3 = x
0 + x
1 + x
2 + x
3
y
4 = x
0 + x
1 + x
2 + x
3 + x
4
... ... ...
The default operation is to add the elements up, but a different operation can be specified as
binary_op instead.
The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
10
11
template <class InputIterator, class OutputIterator>
OutputIterator partial_sum ( InputIterator first, InputIterator last,
OutputIterator result )
{
iterator_traits<InputIterator>::value_type val;
*result++ = val = *first++;
while (first!=last)
*result++ = val = val + *first++;
// or: *result++ = val = binary_op(val,*first++) for binary_op version
return result;
}
Parameters
first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which contains all the elements between
first and
last, including the element pointed by
first but not the element pointed by
last.
result
Output iterator to the initial position in the destination sequence where the partial sums are stored. The range starts at
result and shall have a size large enough to contain as many elements as the range above ( [first,last)).
binary_op
Binary operation taking as arguments two elements of the type pointed by the
InputIterator, and returning the result of the replacement for the sum operation. This can either be a pointer to a function or an object whose class overloads operator().
Return value An iterator pointing to past the last element of the destination sequence where resulting elements have been stored.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// partial_sum example
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int myop (int x, int y) {return x+y+1;}
int main () {
int val[] = {1,2,3,4,5};
int result[5];
partial_sum (val, val+5, result);
cout << "using default partial_sum: ";
for (int i=0; i<5; i++) cout << result[i] << ' ';
cout << endl;
partial_sum (val, val+5, result, multiplies<int>());
cout << "using functional operation multiplies: ";
for (int i=0; i<5; i++) cout << result[i] << ' ';
cout << endl;
partial_sum (val, val+5, result, myop);
cout << "using custom function: ";
for (int i=0; i<5; i++) cout << result[i] << ' ';
cout << endl;
return 0;
}
#include
int myop (int x, int y) {return x+y+1;}
int main () { int val[] = {1,2,3,4,5}; int result[5];
partial_sum (val, val+5, result); cout << "using default partial_sum: "; for (int i=0; i<5; i++) cout << result[i] << ' '; cout << endl;
partial_sum (val, val+5, result, multiplies
partial_sum (val, val+5, result, myop); cout << "using custom function: "; for (int i=0; i<5; i++) cout << result[i] << ' '; cout << endl; return 0; }
Output:
using default partial_sum: 1 3 6 10 15
using functional operation multiplies: 1 2 6 24 120
using custom function: 1 4 8 13 19
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
콜백 함수를 Angular 하위 구성 요소에 전달이 예제는 구성 요소에 함수를 전달하는 것과 관련하여 최근에 직면한 문제를 다룰 것입니다. 국가 목록을 제공하는 콤보 상자 또는 테이블 구성 요소. 지금까지 모든 것이 구성 요소 자체에 캡슐화되었으며 백엔드에 대한 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.