std::inner_product

来自cppreference.com
< cpp‎ | algorithm
 
 
算法库
受约束算法及范围上的算法 (C++20)
受约束算法: std::ranges::copy, std::ranges::sort, ...
执行策略 (C++17)
不修改序列的操作
(C++11)(C++11)(C++11)
(C++17)
修改序列的操作
未初始化存储上的操作
划分操作
排序操作
(C++11)
二分搜索操作
集合操作(在已排序范围上)
堆操作
(C++11)
最小/最大操作
(C++11)
(C++17)

排列
数值运算
(C++11)
inner_product
C 库
 
在标头 <numeric> 定义
(1)
template< class InputIt1, class InputIt2, class T >
T inner_product( InputIt1 first1, InputIt1 last1, InputIt2 first2, T init );
(C++20 前)
template< class InputIt1, class InputIt2, class T >

constexpr T inner_product( InputIt1 first1, InputIt1 last1,

                           InputIt2 first2, T init );
(C++20 起)
(2)
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 );
(C++20 前)
template< class InputIt1, class InputIt2, class T,

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

                           BinaryOperation1 op1, BinaryOperation2 op2 );
(C++20 起)

计算内积(即积之和)或在范围 [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 不能使涉及范围的任何迭代器(包含尾迭代器)失效,或修改该范围的任何元素。

参数

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

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

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

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

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

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

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

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

类型要求
-
InputIt1, InputIt2 必须符合老式输入迭代器 (LegacyInputIterator) 的要求。
-
ForwardIt1, ForwardIt2 必须符合老式向前迭代器 (LegacyForwardIterator) 的要求。
-
T 必须符合可复制赋值 (CopyAssignable) 可复制构造 (CopyConstructible) 的要求。

返回值

上述 acc 的最终值。

可能的实现

版本一
template<class InputIt1, class InputIt2, class T>
constexpr // C++20 起
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>
constexpr // C++20 起
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 <numeric>
#include <iostream>
#include <vector>
#include <functional>
 
int main()
{
    std::vector<int> a{0, 1, 2, 3, 4};
    std::vector<int> b{5, 4, 2, 3, 1};
 
    int r1 = std::inner_product(a.begin(), a.end(), b.begin(), 0);
    std::cout << "a 和 b 的内积:" << r1 << '\n';
 
    int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0,
                                std::plus<>(), std::equal_to<>());
    std::cout << "a 和 b 中匹配的对数:" <<  r2 << '\n';
}

输出:

a 和 b 的内积:21
a 和 b 中匹配的对数:2

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 242 C++98 op1op2 不能有任何副作用 它们不能修改涉及到的范围

参阅

应用一个函数对象,然后以乱序规约
(函数模板)
对一个范围内的元素求和
(函数模板)
计算范围内元素的部分和
(函数模板)