Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/algorithm/partial sum"

From cppreference.com
< cpp‎ | algorithm
(Example: formatting)
(Ditto, not DR)
 
(47 intermediate revisions by 16 users not shown)
Line 1: Line 1:
 
{{cpp/title|partial_sum}}
 
{{cpp/title|partial_sum}}
{{cpp/algorithm/sidebar}}
+
{{cpp/algorithm/numeric/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | numeric}}
+
{{dcl header|numeric}}
{{ddcl list item | num=1 |
+
{{dcla|num=1|notes={{mark constexpr since c++20}}|
template< class InputIterator, class OutputIterator >
+
template< class InputIt, class OutputIt >
OutputIterator partial_sum( InputIterator first, InputIterator last, OutputIterator d_first );
+
OutputIt partial_sum( InputIt first, InputIt last,
 +
                      OutputIt d_first );
 
}}
 
}}
{{ddcl list item | num=2 |
+
{{dcla|num=2|notes={{mark constexpr since c++20}}|
template< class InputIterator, class OutputIterator, class BinaryOperation >
+
template< class InputIt, class OutputIt, class BinaryOp >
OutputIterator partial_sum( InputIterator first, InputIterator last, OutputIterator d_first,
+
OutputIt partial_sum( InputIt first, InputIt last,
                            BinaryOperation op );
+
                      OutputIt d_first, BinaryOp op );
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
Computes the partial sums of the elements in the subranges of the range {{tt|[first, last)}} and writes them to the range beginning at {{tt|d_first}}. The first version uses {{tt|operator+}} to sum up the elements, the second version uses the given binary function {{tt|op}}.
+
@1@ If {{range|first|last}} is empty, does nothing.
 +
@@ Otherwise, performs the following operations in order:
 +
# Creates an accumulator {{c|acc}}, whose type is the [[cpp/iterator#Types and writability|value type]] of {{tt|InputIt}}, and initializes it with {{c|*first}}.
 +
# Assigns {{c|acc}} to {{c|*d_first}}.
 +
# For each integer {{c|i}} in {{range|1|std::distance(first, last)}}, performs the following operations in order:
 +
::@a@ Computes {{rev inl|until=c++20|{{c|acc + *iter}}}}{{rev inl|since=c++20|{{c|std::move(acc) + *iter}}}}, where {{c|iter}} is the next {{c|i}}{{su|b=th}} iterator of {{c|first}}.
 +
::@b@ Assigns the result to {{c|acc}}.
 +
::@c@ Assigns {{c|acc}}<ref>The actual value to be assigned is the result of the assignment in the previous step. We assume the assignment result is {{c|acc}} here.</ref> to {{c|*dest}}, where {{c|dest}} is the next {{c|i}}{{su|b=th}} iterator of {{c|d_first}}.
  
Equivalent operation:
+
@2@ Same as {{v|1}}, but computes {{rev inl|until=c++20|{{c|op(acc, *iter)}}}}{{rev inl|since=c++20|{{c|op(std::move(acc), *iter)}}}} instead.
 +
 
 +
Given {{c|binary_op}} as the actual binary operation:
 +
 
 +
* If any of the following conditions is satisfied, the program is ill-formed:
 +
:* The value type of {{tt|InputIt}} is not constructible from {{c|*first}}.
 +
:* {{c|acc}} is not [[cpp/iterator#Types and writability|writable]] to {{c|d_first}}.
 +
:* The result of {{rev inl|until=c++20|{{c|binary_op(acc, *iter)}}}}{{rev inl|since=c++20|{{c|binary_op(std::move(acc), *iter)}}}} is not implicitly convertible to the value type of {{tt|InputIt}}.
 +
 
 +
* Given {{c|d_last}} as the iterator to be [[#Return value|returned]], if any of the following conditions is satisfied, the behavior is undefined:
 +
:* {{c|binary_op}} modifies any element of {{range|first|last}} or {{range|d_first|d_last}}.
 +
:* {{c|binary_op}} invalidates any iterator or subrange in {{closed range|first|last}} or {{closed range|d_first|d_last}}.
 +
 
 +
 
 +
<references/>
  
{{source cpp|1=
 
*(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);
 
...
 
}}
 
 
===Parameters===
 
===Parameters===
{{param list begin}}
+
{{par begin}}
{{param list item | first, last | the range of elements to sum}}
+
{{par|first, last|the range of elements to sum}}
{{param list item | d_first | the beginning of the destination range}}
+
{{par|d_first|the beginning of the destination range; may be equal to {{c|first}}}}
{{param list op2 | op | t1=iterator_traits<InputIterator>::value_type | p2=InputIterator | rt=iterator_traits<InputIterator>::value_type}}
+
{{par op2|op|t1=std::iterator_traits<InputIt>::value_type|p2=InputIt|rp=InputIt}}
{{param list end}}
+
{{par hreq}}
 +
{{par req named|InputIt|InputIterator}}
 +
{{par req named|OutputIt|OutputIterator}}
 +
{{par end}}
  
 
===Return value===
 
===Return value===
Iterator to the element past the last element written.
+
Iterator to the element past the last element written, or {{c|d_first}} if {{range|first|last}} is empty.
  
 
===Complexity===
 
===Complexity===
Exactly {{tt|(last - first) - 1}} applications of the binary operation
+
Given {{mathjax-or|\(\scriptsize N\)|N}} as {{c|std::distance(first, last)}}:
 
+
@1@ Exactly {{mathjax-or|\(\scriptsize N-1\)|N-1}} applications of {{c/core|operator+}}.
===Equivalent function===
+
@2@ Exactly {{mathjax-or|\(\scriptsize N-1\)|N-1}} applications of the binary function {{c|op}}.
{{eq fun cpp
+
| 1=
+
===Possible implementation===
template<class InputIterator, class OutputIterator>
+
{{eq impl
OutputIterator partial_sum(InputIterator first, InputIterator last,  
+
|title1=partial_sum (1)|ver1=1|1=
                          OutputIterator d_first)
+
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;
+
     if (first == last)
 
+
        return d_first;
     typename std::iterator_traits<InputIterator>::value_type sum = *first;
+
   
 +
     typename std::iterator_traits<InputIt>::value_type sum = *first;
 
     *d_first = sum;
 
     *d_first = sum;
 
+
   
     while (++first != last) {
+
     while (++first != last)
      sum = sum + *first;
+
    {
      *++d_first = sum;
+
        sum = std::move(sum) + *first; // std::move since C++20
 +
        *++d_first = sum;
 
     }
 
     }
 +
   
 
     return ++d_first;
 
     return ++d_first;
 +
   
 +
    // or, since C++14:
 +
    // return std::partial_sum(first, last, d_first, std::plus<>());
 
}
 
}
| 2 =  
+
|title2=partial_sum (2)|ver2=2|2=
template<class InputIterator, class OutputIterator, class BinaryOperator>
+
template<class InputIt, class OutputIt, class BinaryOp>
OutputIterator partial_sum(InputIterator first, InputIterator last,  
+
constexpr // since C++20
                          OutputIterator d_first, BinaryOperation op)
+
OutputIt partial_sum(InputIt first, InputIt last,  
 +
                    OutputIt d_first, BinaryOp op)
 
{
 
{
     if (first == last) return d_first;
+
     if (first == last)
 
+
        return d_first;
     typename std::iterator_traits<InputIterator>::value_type sum = *first;
+
   
     *d_first = sum;
+
     typename std::iterator_traits<InputIt>::value_type acc = *first;
 
+
     *d_first = acc;
     while (++first != last) {
+
   
      sum = op(sum, *first);
+
     while (++first != last)
      *++d_first = sum;
+
    {
 +
        acc = op(std::move(acc), *first); // std::move since C++20
 +
        *++d_first = acc;
 
     }
 
     }
 +
   
 
     return ++d_first;
 
     return ++d_first;
 
}
 
}
 +
}}
 +
 +
===Notes===
 +
{{c|acc}} was introduced because of the resolution of {{lwg|539}}. The reason of using {{c|acc}} rather than directly summing up the results (i.e. {{c|1=*(d_first + 2) = (*first + *(first + 1)) + *(first + 2);}}) is because the semantic of the latter is confusing if the following types mismatch:
 +
* the value type of {{tt|InputIt}}
 +
* the writable type(s) of {{tt|OutputIt}}
 +
* the types of the parameters of {{c/core|operator+}} or {{c|op}}
 +
* the return type of {{c/core|operator+}} or {{c|op}}
 +
 +
{{c|acc}} serves as the intermediate object to store and provide the values for each step of the computation:
 +
* its type is the value type of {{tt|InputIt}}
 +
* it is written to {{c|d_first}}
 +
* its value is passed to {{c/core|operator+}} or {{c|op}}
 +
* it stores the return value of {{c/core|operator+}} or {{c|op}}
 +
 +
{{source|1=
 +
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>{});
 
}}
 
}}
  
 
===Example===
 
===Example===
{{example cpp
+
{{example
|
+
|
| code=
+
|code=
#include <numeric>
+
#include <functional>
#include <vector>
+
 
#include <iostream>
 
#include <iostream>
 
#include <iterator>
 
#include <iterator>
#include <functional>
+
#include <numeric>
 +
#include <vector>
 +
 
 
int main()
 
int main()
 
{
 
{
     std::vector<int> v = {2,2,2,2,2,2,2,2,2,2};
+
     std::vector<int> v(10, 2); // v = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
 
+
   
     std::cout << "The first 10 even numbers are: ";
+
     std::cout << "The first " << v.size() << " even numbers are: ";
     std::partial_sum(v.begin(), v.end(),  
+
    // write the result to the cout stream
 +
     std::partial_sum(v.cbegin(), v.cend(),  
 
                     std::ostream_iterator<int>(std::cout, " "));
 
                     std::ostream_iterator<int>(std::cout, " "));
 
     std::cout << '\n';
 
     std::cout << '\n';
 
+
   
     std::partial_sum(v.begin(), v.end(), v.begin(), std::multiplies<int>());
+
    // write the result back to the vector v
     std::cout << "The first 10 powers of 2 are: ";
+
     std::partial_sum(v.cbegin(), v.cend(),
     for(auto n: v) {
+
                    v.begin(), std::multiplies<int>());
         std::cout << n << " ";
+
   
    }
+
     std::cout << "The first " << v.size() << " powers of 2 are: ";
 +
     for (int n : v)
 +
         std::cout << n << ' ';
 
     std::cout << '\n';
 
     std::cout << '\n';
 
}
 
}
| output=
+
|output=
 
The first 10 even numbers are: 2 4 6 8 10 12 14 16 18 20  
 
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  
 
The first 10 powers of 2 are: 2 4 8 16 32 64 128 256 512 1024  
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=242|std=C++98|before={{c|op}} could not have side effects|after=it cannot modify the ranges involved}}
 +
{{dr list item|wg=lwg|dr=539|std=C++98|before=the type requirements needed for the result<br>evaluations and assignments to be valid were missing|after=added}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/algorithm/dcl list adjacent_difference}}
+
{{dsc inc|cpp/algorithm/dsc adjacent_difference}}
{{dcl list template | cpp/algorithm/dcl list accumulate}}
+
{{dsc inc|cpp/algorithm/dsc accumulate}}
{{dcl list end}}
+
{{dsc inc|cpp/algorithm/dsc inclusive_scan}}
 +
{{dsc inc|cpp/algorithm/dsc exclusive_scan}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 05:02, 14 September 2024

 
 
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy, ranges::sort, ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
(C++11)                (C++11)(C++11)

Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17)(C++11)
(C++20)(C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
(C++11)
(C++17)
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
partial_sum
(C++17)   
Operations on uninitialized memory
 
 
Defined in header <numeric>
template< class InputIt, class OutputIt >

OutputIt partial_sum( InputIt first, InputIt last,

                      OutputIt d_first );
(1) (constexpr since C++20)
template< class InputIt, class OutputIt, class BinaryOp >

OutputIt partial_sum( InputIt first, InputIt last,

                      OutputIt d_first, BinaryOp op );
(2) (constexpr since C++20)
1) If [firstlast) is empty, does nothing.
Otherwise, performs the following operations in order:
  1. Creates an accumulator acc, whose type is the value type of InputIt, and initializes it with *first.
  2. Assigns acc to *d_first.
  3. For each integer i in [1std::distance(first, last)), performs the following operations in order:
a) Computes acc + *iter(until C++20)std::move(acc) + *iter(since C++20), where iter is the next ith iterator of first.
b) Assigns the result to acc.
c) Assigns acc[1] to *dest, where dest is the next ith iterator of d_first.
2) Same as (1), but computes op(acc, *iter)(until C++20)op(std::move(acc), *iter)(since C++20) instead.

Given binary_op as the actual binary operation:

  • If any of the following conditions is satisfied, the program is ill-formed:
  • The value type of InputIt is not constructible from *first.
  • acc is not writable to d_first.
  • The result of binary_op(acc, *iter)(until C++20)binary_op(std::move(acc), *iter)(since C++20) is not implicitly convertible to the value type of InputIt.
  • Given d_last as the iterator to be returned, if any of the following conditions is satisfied, the behavior is undefined:
  • binary_op modifies any element of [firstlast) or [d_firstd_last).
  • binary_op invalidates any iterator or subrange in [firstlast] or [d_firstd_last].


  1. The actual value to be assigned is the result of the assignment in the previous step. We assume the assignment result is acc here.

Contents

[edit] Parameters

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 std::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 InputIt can be dereferenced and assigned a value of type Ret. ​

Type requirements
-
InputIt must meet the requirements of LegacyInputIterator.
-
OutputIt must meet the requirements of LegacyOutputIterator.

[edit] Return value

Iterator to the element past the last element written, or d_first if [firstlast) is empty.

[edit] Complexity

Given N as std::distance(first, last):

1) Exactly N-1 applications of operator+.
2) Exactly N-1 applications of the binary function op.

[edit] Possible implementation

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;
}

[edit] Notes

acc was introduced because of the resolution of LWG issue 539. The reason of using acc rather than directly summing up the results (i.e. *(d_first + 2) = (*first + *(first + 1)) + *(first + 2);) is because the semantic of the latter is confusing if the following types mismatch:

  • the value type of InputIt
  • the writable type(s) of OutputIt
  • the types of the parameters of operator+ or op
  • the return type of operator+ or op

acc serves as the intermediate object to store and provide the values for each step of the computation:

  • its type is the value type of InputIt
  • it is written to d_first
  • its value is passed to operator+ or op
  • it stores the return value of operator+ or 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>{});

[edit] Example

#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';
}

Output:

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

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 242 C++98 op could not have side effects it cannot modify the ranges involved
LWG 539 C++98 the type requirements needed for the result
evaluations and assignments to be valid were missing
added

[edit] See also

computes the differences between adjacent elements in a range
(function template) [edit]
sums up or folds a range of elements
(function template) [edit]
similar to std::partial_sum, includes the ith input element in the ith sum
(function template) [edit]
similar to std::partial_sum, excludes the ith input element from the ith sum
(function template) [edit]