C++11标准模板(STL)- 算法库 - 类似 std::partial_sum,第 i 个和中排除第 i 个输入 -(std::exclusive_scan)

算法库

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

类似 std::partial_sum,第 i 个和中排除第 i 个输入

std::exclusive_scan
template< class InputIt, class OutputIt, class T >

OutputIt exclusive_scan( InputIt first, InputIt last,

                         OutputIt d_first, T init );
(1)(C++17 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class T >

ForwardIt2 exclusive_scan( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                           ForwardIt2 d_first, T init);
(2)(C++17 起)
template< class InputIt, class OutputIt,

          class T, class BinaryOperation >
OutputIt exclusive_scan( InputIt first, InputIt last,

                         OutputIt d_first, T init, BinaryOperation binary_op );
(3)(C++17 起)
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2,

          class T, class BinaryOperation >
ForwardIt2 exclusive_scan( ExecutionPolicy&& policy, ForwardIt1 first, ForwardIt1 last,

                           ForwardIt2 d_first, T init, BinaryOperation binary_op );
(4)(C++17 起)

binary_op (或对于重载 (1-2) 是 std::plus<>() )计算范围 [first, last) 上排除性前缀和,以 init 为初始值,并写入结果到从 d_first 开始的范围。“排除性”表示第 i 个输入元素不包含于第 i 个和。

正式而言,用对于 [first, first + (i - d_first)) 中的每个 jbinary_op 上的广义非交换和 init, *j... 的值,通过 [d_first, d_first + (last - first)) 中的每个迭代器赋值。

其中广义非交换和 GNSUM(op, a
1, ..., a
N) 定义如下:

  • 若 N=1 ,则为 a
    1
  • 若 N > 1 ,则为 op(GNSUM(op, a
    1, ..., a
    K), GNSUM(op, a
    M, ..., a
    N)) ,对于任何 1 < K+1 = M ≤ N 中的 K

换言之,和运算可能以任意顺序进行,而且若 binary_op 非结合,则行为是非确定的。

重载 (2,4) 按照 policy 执行。此重载仅若 std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 为 true 才参与重载决议

binary_op 不应非法化范围 [first, last) 或 [d_first, d_first + (last - first)) 中的迭代器(含尾迭代器)或子范围,亦不应修改其中的元素。否则行为未定义。

参数

first, last-要求和的元素范围
d_first-目标范围的起始;可以等于 first
policy-所用的执行策略。细节见执行策略。
init-初始值
binary_op-将应用到解引用输入迭代器的结果、其他 binary_op 的结果和 init 的二元函数对象 (FunctionObject) 。
类型要求
- InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- OutputIt 必须满足遗留输出迭代器 (LegacyOutputIterator) 的要求。
- ForwardIt1, ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
- T 必须满足可移动构造 (MoveConstructible) 的要求。且 binary_op(init, *first)binary_op(init, init)binary_op(*first, *first) 必须能转换到 T

返回值

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

复杂度

O(last - first) 次应用二元运算

异常

拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:

  • 若作为算法一部分调用的函数的执行抛出异常,且 ExecutionPolicy 为标准策略之一,则调用 std::terminate 。对于任何其他 ExecutionPolicy ,行为是实现定义的。
  • 若算法无法分配内存,则抛出 std::bad_alloc 。

调用示例

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <time.h>
#include <random>
#include <vector>
#include <cassert>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}

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

    friend Cell operator+(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x += rcell.x;
        cell.y += rcell.y;
        return cell;
    }

    friend Cell operator-(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x -= rcell.x;
        cell.y -= rcell.y;
        return cell;
    }

    friend Cell operator*(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x *= rcell.x;
        cell.y *= rcell.y;
        return cell;
    }

    friend Cell operator/(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x /= rcell.x;
        cell.y /= rcell.y;
        return cell;
    }

    friend Cell operator%(const Cell &lcell, const Cell &rcell)
    {
        Cell cell = lcell;
        cell.x %= rcell.x;
        cell.y %= rcell.y;
        return cell;
    }
};

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

namespace std
{
template<class InputIt, class OutputIt, class T, class BinaryOperation>
OutputIt exclusive_scan(InputIt first, InputIt last, OutputIt d_first, T init, BinaryOperation binary_op)
{
    if (first == last)
    {
        return d_first;
    }
    T temp = init;
    *d_first = temp;
    ++d_first;
    for (++first; first != last; ++first, ++d_first)
    {
        temp = binary_op(temp, *first);
        *d_first = temp;
    }
    return d_first;
}
}

int main()
{
    std::cout << std::boolalpha;

    std::mt19937 g{std::random_device{}()};
    srand((unsigned)time(NULL));

    auto generate = []()
    {
        int n = std::rand() % 10 + 110;
        Cell cell{n, n};
        return cell;
    };

    auto generate10 = []()
    {
        int n = std::rand() % 10 + 1;
        Cell cell{n, n};
        return cell;
    };

    //3) 构造拥有 count 个有值 value 的元素的容器。
    std::vector<Cell> vector1(8, generate());
    std::generate(vector1.begin(), vector1.end(), generate);
    std::sort(vector1.begin(), vector1.end());
    std::cout << "vector1:  ";
    std::copy(vector1.begin(), vector1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::vector<Cell> vector2(vector1.size(), Cell{101, 101});
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::vector<Cell> vector3(8, generate10());
    std::generate(vector3.begin(), vector3.end(), generate10);
    std::sort(vector3.begin(), vector3.end());
    std::cout << "vector3:  ";
    std::copy(vector3.begin(), vector3.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;

    std::exclusive_scan(vector1.begin(), vector1.end(), vector2.begin(), *vector1.begin(), std::plus<Cell>());
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    std::exclusive_scan(vector1.begin(), vector1.end(), vector2.begin(), *vector1.begin(), std::minus<Cell>());
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    std::exclusive_scan(vector3.begin(), vector3.end(), vector2.begin(), *vector3.begin(), std::multiplies<Cell>());
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    std::exclusive_scan(vector1.begin(), vector1.end(), vector2.begin(), Cell{1024, 1024}, std::divides<Cell>());
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    std::exclusive_scan(vector1.begin(), vector1.end(), vector2.begin(), Cell{1024, 1024}, std::modulus<Cell>());
    std::cout << "vector2:  ";
    std::copy(vector2.begin(), vector2.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

输出

vector1:  {113,113} {114,114} {115,115} {117,117} {117,117} {118,118} {118,118} {118,118}
vector2:  {101,101} {101,101} {101,101} {101,101} {101,101} {101,101} {101,101} {101,101}
vector3:  {3,3} {3,3} {5,5} {5,5} {6,6} {7,7} {7,7} {8,8}
vector2:  {113,113} {227,227} {342,342} {459,459} {576,576} {694,694} {812,812} {930,930}

vector2:  {113,113} {-1,-1} {-116,-116} {-233,-233} {-350,-350} {-468,-468} {-586,-586} {-704,-704}

vector2:  {3,3} {9,9} {45,45} {225,225} {1350,1350} {9450,9450} {66150,66150} {529200,529200}

vector2:  {1024,1024} {8,8} {0,0} {0,0} {0,0} {0,0} {0,0} {0,0}

vector2:  {1024,1024} {112,112} {112,112} {112,112} {112,112} {112,112} {112,112} {112,112}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值