.\" ident @(#)partial_sum.3 .\" Standard Template Library .\" $$RW_INSERT_HEADER "slyrs.man" .TH partial_sum 3C++ "02 Apr 1998" "Rogue Wave Software" "-" .ce2 Standard C++ Library Copyright 1998, Rogue Wave Software, Inc. .SH NAME \f2partial_sum\fP \ - Calculates successive partial sums of a range of values. .SH SYNOPSIS .RE .RS 0 #include .br template .br OutputIterator partial_sum (InputIterator first, .RE .RS 27 InputIterator last, .br OutputIterator result); .RE .RS 0 .br template .RE .RS 0 OutputIterator partial_sum (InputIterator first, .RE .RS 27 InputIterator last, .br OutputIterator result, .br BinaryOperation binary_op); .SH DESCRIPTION The partial_sum algorithm creates a new sequence in which every element is formed by adding all the values of the previous elements, or, in the second form of the algorithm, by applying the operation \f2binary_op\fP successively on every previous element. That is, partial_sum assigns to every iterator \f2i\fP in the range \f2[result, result + (last - first))\fP a value equal to: .RE .RS 0 ((...(*first + *(first + 1)) + ... ) + .RE .RS 6 *(first + (i - result))) .RE or, in the second version of the algorithm: .RE .RS 0 binary_op(binary_op(..., binary_op (*first, .RE .RS 10 *(first + 1)),...),*(first + (i - result))) .RE For instance, applying partial_sum to (1,2,3,4,) yields (1,3,6,10). The partial_sum algorithm returns \f2result\fP \f2+ (last - first)\fP. If \f2result\fP is equal to \f2first\fP, the elements of the new sequence successively replace the elements in the original sequence, effectively turning partial_sum into an inplace transformation. .SH COMPLEXITY Exactly \f2(last - first) - 1\fP applications of the default \f2+\fP operator or \f2binary_op\fP are performed. .SH EXAMPLE .RE .RS 0 // .br // partsum.cpp .br // .RE .RS 1 #include //for accumulate .br #include //for vector .br #include //for times .br #include .RE .RS 0 using namespace std; .br .br int main() .RE .RS 1 { .RE .RS 3 //Initialize a vector using an array of ints .RE .RS 2 int d1[10] = {1,2,3,4,5,6,7,8,9,10}; .br vector v(d1, d1+10); .RE .RS 0 .RE .RS 3 //Create an empty vectors to store results .RE .RS 2 vector sums((size_t)10), prods((size_t)10); .RE .RS 0 .RE .RS 3 //Compute partial_sums and partial_products .br partial_sum(v.begin(), v.end(), sums.begin()); .br partial_sum(v.begin(), v.end(), prods.begin(), .RE .RS 14 times()); .RE .RS 3 //Output the results .RE .RS 2 cout << "For the series: " << endl << " "; .br copy(v.begin(),v.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << endl << endl; .RE .RS 0 .RE .RS 2 cout << "The partial sums: " << endl << " " ; .br copy(sums.begin(),sums.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout <<" should each equal (N*N + N)/2" << endl << endl; .RE .RS 0 .RE .RS 2 cout << "The partial products: " << endl << " "; .br copy(prods.begin(),prods.end(), .RE .RS 7 ostream_iterator(cout," ")); .RE .RS 2 cout << " should each equal N!" << endl; .RE .RS 0 .RE .RS 2 return 0; .RE .RS 1 } .br .RE .RS 0 Program Output .RE .RS 0 .br For the series: .RE .RS 1 1 2 3 4 5 6 7 8 9 10 .RE .RS 0 The partial sums: .RE .RS 1 1 3 6 10 15 21 28 36 45 55 should each equal (N*N + N)/2 .RE .RS 0 The partial products: .RE .RS 1 1 2 6 24 120 720 5040 40320 362880 3628800 should each equal N! .SH WARNINGS If your compiler does not support default template parameters, then you always need to include the \f2Allocator\fP template argument. For instance, you need to write: \f2vector >\fP instead of: \f2vector\fP If your compiler does not support namespaces, then you do not need the using declaration for \f2std\fP.