std::inner_product
| 定义于头文件 <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) 和从 std::distance(first1, last1) 开始的 first2 元素范围上进行有序的映射/规约操作。
acc(类型为 T)初始化累加器 init,然后按顺序对范围 acc = acc + (*i1) * (*i2)(C++20 前)acc = std::move(acc) + (*i1) * (*i2)(C++20 起) 中的每个迭代器 i1 及其对应的在从 [first1, last1) 开始的范围内的迭代器 i2 逐一求值表达式 first2。对于 + 和 * 的内置含义,这计算两个范围的内积。acc(类型为 T)初始化累加器 init,然后按顺序对范围 acc = op1(acc, op2(*i1, *i2))(C++20 前)acc = op1(std::move(acc), op2(*i1, *i2))(C++20 起) 中的每个迭代器 i1 及其对应的在从 [first1, last1) 开始的范围内的迭代器 i2 逐一求值表达式 first2。给定 last2 作为 std::distance(first1, last1) 个的下一个迭代器 first2,若满足以下任何条件,则行为未定义:
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 // 自 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 不能具有副作用 | 它们不能修改所涉及的区间 |
参见
|
(C++17)
|
应用可调用对象后无序规约
(函数模板) |
|
对范围内的元素求和或折叠
(函数模板) |
|
|
计算范围内元素的部分和
(函数模板) |