C++11标准模板(STL)- 算法 - 数值运算(std::partial_sum)

定义于头文件  <numeric>

算法库提供大量用途的函数(例如查找、排序、计数、操作),它们在元素范围上操作。注意范围定义为 [first, last) ,其中 last 指代要查询或修改的最后元素的后一个元素。

计算范围内元素的部分和

std::partial_sum

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 对元素求和,均将 std::move 应用到其左侧运算数 (C++20 起)。

等价运算:

*(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 必须无副效应。

(C++11 前)

op 必须不非法化涉及范围的任何迭代器,含尾迭代器,或修改该范围的任何元素。

(C++11 起)

参数

first, last-要求和的元素范围
d_first-目标范围起始;可以等于 first
op-被使用的二元函数对象。

该函数的签名应当等价于:

 Ret fun(const Type1 &a, const Type2 &b);

签名中并不需要有 const &。
类型 Type1 必须使得 iterator_traits<InputIt>::value_type 类型的对象能隐式转换到 Type1 。类型 Type2 必须使得 InputIt 类型的对象能在解引用后隐式转换到 Type2 。 类型 Ret 必须使得 iterator_traits<InputIt>::value_type 类型对象能被赋 Ret 类型值。 ​

类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。

返回值

指向最后被写入元素后一元素的迭代器。

复杂度

恰好应用 (last - first) - 1 次二元运算

可能的实现

版本一

template<class InputIt, class OutputIt>
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; // C++20 起有 std::move
       *++d_first = sum;
    }
    return ++d_first;
 
    // 或 C++14 起:
    // return std::partial_sum(first, last, d_first, std::plus<>());
}

版本二

template<class InputIt, class OutputIt, class BinaryOperation>
OutputIt partial_sum(InputIt first, InputIt last, 
                     OutputIt d_first, BinaryOperation op)
{
    if (first == last) return d_first;
 
    typename std::iterator_traits<InputIt>::value_type sum = *first;
    *d_first = sum;
 
    while (++first != last) {
       sum = op(std::move(sum), *first); // C++20 起有 std::move
       *++d_first = sum;
    }
    return ++d_first;
}

调用示例

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <iterator>
#include <time.h>

using namespace std;

struct Cell
{
    int x;
    int y;

    Cell &operator +=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator +(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator *(const Cell &cell)
    {
        x *= cell.x;
        y *= cell.y;
        return *this;
    }

    Cell &operator ++()
    {
        x += 1;
        y += 1;
        return *this;
    }

    bool operator <(const Cell &cell) const
    {
        if (x == cell.x)
        {
            return y < cell.y;
        }
        else
        {
            return x < cell.x;
        }
    }

    bool operator ==(const Cell &cell) const
    {
        return x == cell.x && y == cell.y;
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

int main()
{
    std::mt19937 g{std::random_device{}()};

    srand((unsigned)time(NULL));;

    std::cout << std::boolalpha;

    std::function<Cell()> generate = []()
    {
        int n = std::rand() % 10 + 100;
        Cell cell{n, n};
        return cell;
    };

    // 初始化lCells1
    std::list<vector<Cell>> lCells1(5, vector<Cell>(5));
    //用从起始值开始连续递增的值填充一个范围
    for (vector<Cell> &vCells : lCells1)
    {
        std::iota(vCells.begin(), vCells.end(), Cell(generate()));
    }

    size_t index = 0;
    for (vector<Cell> &vCells : lCells1)
    {
        std::cout << "cell  " << index << "       ";
        std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        std::vector<Cell> pCells(vCells.size());
        //计算范围 [first, last) 的子范围中元素的部分和,并写入到始于 d_first 的范围。
        //第一版本用 operator+
        std::partial_sum(vCells.begin(), vCells.end(), pCells.begin());
        std::cout << "partial_sum   ";
        std::copy(pCells.begin(), pCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        index ++;
    }

    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << std::endl;


    auto opt = [](const Cell & a, const Cell & b)
    {
        Cell cell{a.x + b.x, a.y + b.y};
        return cell;
    };

    // 初始化lCells2
    std::list<vector<Cell>> lCells2(5, vector<Cell>(5));
    //用从起始值开始连续递增的值填充一个范围
    for (vector<Cell> &vCells : lCells2)
    {
        std::iota(vCells.begin(), vCells.end(), Cell(generate()));
    }

    index = 0;
    for (vector<Cell> &vCells : lCells2)
    {
        std::cout << "cell  " << index << "       ";
        std::copy(vCells.begin(), vCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        std::vector<Cell> pCells(vCells.size());
        //计算范围 [first, last) 的子范围中元素的部分和,并写入到始于 d_first 的范围。
        //第二版本用给定的二元函数 op 对元素求和
        std::partial_sum(vCells.begin(), vCells.end(), pCells.begin(), opt);
        std::cout << "partial_sum   ";
        std::copy(pCells.begin(), pCells.end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;
        index ++;
    }

    return 0;
}

输出

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值