boost::partial_sum

References

Headers

boost::partial_sum is available by including the following header:

  • boost/range/numeric.hpp

Examples

partial_sum.cpp

#include <functional>
#include <iostream>
#include <vector>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::vector<int> sums;
    std::vector<int> products;

    // Straight-forward wrapper around std::partial_sum().
    // Returns an iterator to the end of the output range.
    boost::partial_sum(vec, std::back_inserter(sums));
    boost::partial_sum(vec, std::back_inserter(products), std::multiplies<int>());

    std::cout << "Sums(1..5): ";
    boost::copy(sums, std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;

    std::cout << "Products(1..5): ";
    boost::copy(products, std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
    return 0;
}

Output:

Sums(1..5): 1 3 6 10 15 
Products(1..5): 1 2 6 24 120

 

Boost Range for Humans

This reference is part of Boost Range for Humans. Click the link to the overview.