std::inner_product
定义于头文件 <numeric> |
||
template< class InputIt1, class InputIt2, class T > T inner_product( InputIt1 first1, InputIt1 last1, |
(1) | (C++20 起为 constexpr) |
template< class InputIt1, class InputIt2, class T, class BinaryOp1, class BinaryOp2 > |
(2) | (C++20 起为 constexpr) |
计算内积(即乘积之和)或对范围 [
first1,
last1)
以及以 first2 开头的 std::distance(first1, last1) 个元素的范围执行有序的映射/归约操作。
T
),然后按顺序对范围 [
first1,
last1)
中的每个迭代器 i1 及其在以 first2 开头的范围中对应的迭代器 i2,使用表达式 acc = acc + (*i1) * (*i2)(C++20 前)acc = std::move(acc) + (*i1) * (*i2)(C++20 起) 来修改它。对于 + 和 * 的内置含义,这将计算两个范围的内积。T
),然后按顺序对范围 [
first1,
last1)
中的每个迭代器 i1 及其在以 first2 开头的范围中对应的迭代器 i2,使用表达式 acc = op1(acc, op2(*i1, *i2))(C++20 前)acc = op1(std::move(acc), op2(*i1, *i2))(C++20 起) 来修改它。给定 last2 作为 first2 的 std::distance(first1, last1) 个下一个迭代器,如果满足以下任何条件,则行为未定义:
-
T
不是 可复制构造的 (CopyConstructible)。 -
T
不是 可复制赋值的 (CopyAssignable)。 - 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 &。 |
op2 | - | 将应用的二元操作函数对象。此“乘积”函数从每个范围取一个值并生成一个新值。 函数的签名应等效于以下内容 Ret fun(const Type1 &a, const Type2 &b); 签名不需要有 const &。 |
类型要求 | ||
-InputIt1, InputIt2 必须满足 LegacyInputIterator 的要求。 |
[编辑] 返回值
所有修改后的 acc。
[编辑] 可能的实现
inner_product (1) |
---|
template<class InputIt1, class InputIt2, class T> constexpr // since C++20 T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init) { while (first1 != last1) { init = std::move(init) + (*first1) * (*first2); // std::move since C++20 ++first1; ++first2; } return init; } |
inner_product (2) |
template<class InputIt1, class InputIt2, class T, class BinaryOp1, class BinaryOp2> constexpr // since 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)); // std::move since C++20 ++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 << "Inner product of a and b: " << r1 << '\n'; int r2 = std::inner_product(a.begin(), a.end(), b.begin(), 0, std::plus<>(), std::equal_to<>()); std::cout << "Number of pairwise matches between a and 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 不得有副作用 | 它们不能修改所涉及的范围 |
[编辑] 另请参阅
(C++17) |
应用一个可调用对象,然后进行乱序归约 (函数模板) |
对一个范围的元素进行求和或折叠 (函数模板) | |
计算一个范围元素的部分和 (函数模板) |