Namespaces
Variants

std:: inner_product

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
C library
Numeric operations
Operations on uninitialized memory
定义于头文件 <numeric>
template < class InputIt1, class InputIt2, class T >

T inner_product ( InputIt1 first1, InputIt1 last1,

InputIt2 first2, T init ) ;
(1) (自 C++20 起 constexpr)
template < class InputIt1, class InputIt2, class T,

class BinaryOp1, class BinaryOp2 >
T inner_product ( InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,

BinaryOp1 op1, BinaryOp2 op2 ) ;
(2) (自 C++20 起 constexpr)

计算内积(即元素乘积之和)或对范围 [ first1 , last1 ) 与起始于 first2 std:: distance ( first1, last1 ) 个元素范围执行有序映射/归约操作。

1) 使用初始值 init 初始化累加器 acc (类型为 T ),随后通过表达式 acc = acc + ( * i1 ) * ( * i2 ) (C++20 前) acc = std :: move ( acc ) + ( * i1 ) * ( * i2 ) (C++20 起) 按顺序对范围 [ first1 , last1 ) 中的每个迭代器 i1 及其在始于 first2 的范围中的对应迭代器 i2 进行累加修改。对于 + 和 * 的内置含义,此操作计算两个范围的内积。
2) 使用初始值 init 初始化累加器 acc (类型为 T ),随后通过表达式 acc = op1 ( acc, op2 ( * i1, * i2 ) ) (C++20 前) acc = op1 ( std :: move ( acc ) , op2 ( * i1, * i2 ) ) (C++20 起) 按顺序对范围 [ first1 , last1 ) 中的每个迭代器 i1 及其在始于 first2 的范围中的对应迭代器 i2 修改该累加器。

给定 last2 作为 std:: distance ( first1, last1 ) th first2 的后继迭代器,若满足以下任意条件,则行为未定义:

  • T 不满足 可复制构造 要求。
  • T 不满足 可复制赋值 要求。
  • op1 op2 修改了 [ first1 , last1 ) [ first2 , last2 ) 中的任何元素。
  • op1 op2 使 [ first1 , last1 ] [ first2 , last2 ] 中的任何迭代器或子范围失效。

目录

参数

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 的要求。

返回值

acc 在所有修改之后。

可能的实现

inner_product (1)
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;
}
inner_product (2)
template<class InputIt1, class InputIt2, class T,
         class BinaryOp1, class BinaryOp2>
constexpr // 自 C++20 起
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init,
                BinaryOp1 op1, BinaryOp2 op2)
{
    while (first1 != last1)
    {
        init = op1(std::move(init), op2(*first1, *first2)); // 自 C++20 起使用 std::move
        ++first1;
        ++first2;
    }
    return init;
}

注释

该算法的并行化版本 std::transform_reduce 要求 op1 op2 满足交换律和结合律,但 std::inner_product 无此要求,且始终按给定顺序执行运算。

示例

#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
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';
}

输出:

Inner product of a and b: 21
Number of pairwise matches between a and b: 2

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 应用于 发布时行为 正确行为
LWG 242 C++98 op1 op2 不能具有副作用 它们不能修改所涉及的区间

参见

应用可调用对象后无序规约
(函数模板)
对范围内的元素求和或折叠
(函数模板)
计算范围内元素的部分和
(函数模板)