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

定义于头文件 <algorithm>

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

计算两个范围的元素的内积

std::inner_product
template< class InputIt1, class InputIt2, class T >

T inner_product( InputIt1 first1, InputIt1 last1,

                 InputIt2 first2, T init );
(1)
template<class InputIt1, class InputIt2, class T,

         class BinaryOperation1, class BinaryOperation2>
T inner_product( InputIt1 first1, InputIt1 last1,
                 InputIt2 first2, T init,
                 BinaryOperation1 op1,

                 BinaryOperation2 op2 );
(2)

计算内积(即积之和)或在范围 [first1, last1) 和始于 first2 的范围上进行有序映射/规约操作。

1) 以初值 init 初始化积累器 acc ,然后

以表达式 acc = acc + *first1 * *first2 修改它,再以表达式 acc = acc + *(first1+1) * *(first2+1) 修改它,以此类推

(C++20 前)

以表达式 acc = std::move(acc) + *first1 * *first2 修改它,再以表达式 acc = std::move(acc) + *(first1+1) * *(first2+1) 修改它,以此类推

(C++20 起)

直至抵达 last1 。对于 + 与 * 的内建含义,此算法计算二个范围的内积。2) 以初值 init 初始化积累器 acc ,然后

以表达式 acc = op1(acc, op2(*first1, *first2)) 修改它,再以表达式 acc = op1(acc, op2(*(first1+1), *(first2+1))) 修改它,以此类推

(C++20 前)

以表达式 acc = op1(std::move(acc), op2(*first1, *first2)) 修改它,再以表达式 acc = op1(std::move(acc), op2(*(first1+1), *(first2+1))) 修改它,以此类推

(C++20 起)

直至抵达 last1

op1op2 必须无副效应。

(C++11 前)

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

(C++11 起)

参数

first1, last1-元素范围
first2-第二个元素范围的起始
init-积的和的初值
op1-被使用的二元函数对象。此“和”函数接收 op2 所返回的值和当前积累器的值,并产生存储于积累器的新值。

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

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

签名中并不需要有 const &。
类型 Type1 与 Type2 必须使得 T 与 Type3 类型的对象各能隐式转换成 Type1 与 Type2 。 类型 Ret 必须使得 T 类型对象能被赋 Ret 类型值。 ​

op2-被使用的二元函数对象。此“积”函数从每个范围接收一个值并产生新值。

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

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

签名中并不需要有 const &。
类型 Type1 与 Type2 必须使得 InputIt1 与 InputIt2 类型的对象在解引用后分别能隐式转换到 Type1 与 Type2 。 类型 Ret 必须使得 Type3 类型对象能被赋 Ret 类型值。 ​

类型要求
- InputIt1, InputIt2 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。
- ForwardIt1, ForwardIt2 必须满足遗留向前迭代器 (LegacyForwardIterator) 的要求。
- T 必须满足可复制赋值 (CopyAssignable) 和 可复制构造 (CopyConstructible) 的要求。

返回值

上述 acc 的最终值。

可能的实现

版本一

template<class InputIt1, class InputIt2, class T>
T inner_product(InputIt1 first1, InputIt1 last1,
                InputIt2 first2, T init)
{
    while (first1 != last1) {
         init = std::move(init) + *first1 * *first2; // C++20 起有 std::move
         ++first1;
         ++first2;
    }
    return init;
}

版本二

template<class InputIt1, class InputIt2,
         class T,
         class BinaryOperation1, class BinaryOperation2>
T inner_product(InputIt1 first1, InputIt1 last1,
                InputIt2 first2, T init,
                BinaryOperation1 op1,
                BinaryOperation2 op2)
{
    while (first1 != last1) {
         init = op1(std::move(init), op2(*first1, *first2)); // C++20 起有 std::move
         ++first1;
         ++first2;
    }
    return init;
}

注意

此算法的可并行版本 std::transform_reduce 要求 op1op2 可交换并可结合,但 std::inner_product 不作这种要求,且始终以给定顺序进行操作。

调用示例

#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;
        Cell cell{n, n};
        return cell;
    };

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

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

    size_t index = 0;
    std::list<vector<Cell>>::iterator it1 = lCells1.begin();
    std::list<vector<Cell>>::iterator it2 = lCells2.begin();
    for (; it1 != lCells1.end() && it2 != lCells2.end(); it1++, it2++)
    {
        std::cout << "lCells1 " << index << " ";
        std::copy((*it1).begin(), (*it1).end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        std::cout << "lCells1 " << index << " ";
        std::copy((*it2).begin(), (*it2).end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        auto inner_product = std::inner_product((*it1).begin(), (*it1).end(), (*it2).begin(), Cell{0, 0});
        std::cout << "inner_product: " << inner_product << std::endl;
        std::cout << std::endl;
        index++;
    }

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

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

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

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

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

    index = 0;
    std::list<vector<Cell>>::iterator it3 = lCells3.begin();
    std::list<vector<Cell>>::iterator it4 = lCells4.begin();
    for (; it3 != lCells3.end() && it4 != lCells4.end(); it3++, it4++)
    {
        std::cout << "lCells1 " << index << " ";
        std::copy((*it3).begin(), (*it3).end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        std::cout << "lCells1 " << index << " ";
        std::copy((*it4).begin(), (*it4).end(), std::ostream_iterator<Cell>(std::cout, " "));
        std::cout << std::endl;

        auto inner_product = std::inner_product((*it3).begin(), (*it3).end(), (*it4).begin(),
                                                Cell{0, 0}, opt1, opt2);
        std::cout << "inner_product: " << inner_product << std::endl;
        std::cout << std::endl;
        index++;
    }

    return 0;
}

输出

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值