std::inner_product
| 受约束算法及范围上的算法 (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 受约束算法: std::ranges::copy, std::ranges::sort, ... | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 执行策略 (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 不修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 修改序列的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 划分操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 排序操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 二分搜索操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 集合操作(在已排序范围上) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 堆操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 最小/最大操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 排列 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 数值运算 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 未初始化存储上的操作 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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, |
(C++20 起) | |
| (2) | ||
|
template< class InputIt1, class InputIt2, class T,
class BinaryOperation1, class BinaryOperation2 > |
(C++20 前) | |
|
template< class InputIt1, class InputIt2, class T,
class BinaryOperation1, class BinaryOperation2 > |
(C++20 起) | |
在范围 [first1, last1) 和从 first2
开始的范围上计算内积(即积之和)或进行有序映射/规约操作。
T 类型的)累加器 acc,然后按顺序对范围
[first1, last1) 的每个迭代器
i1 和它在从 first2 开始的范围中对应的迭代器 i2 通过 acc = acc + (*i1) *
(*i2) (C++11 前)acc = std::move(acc) +
(*i1)
* (*i2)
(C++11 起) 进行累加。对于
+ 与 * 的内建含义,此算法计算两个范围的内积。T 类型的)累加器 acc,然后按顺序对范围
[first1, last1) 的每个迭代器
i1 和它在从 first2 开始的范围中对应的迭代器 i2 通过 acc = op1(acc, op2(*i1, *i2))
(C++11 前)acc = op1(std::move(acc), op2(*i1, *i2))
(C++11 起) 进行累加。op1 和 op2 不能使涉及范围的任何迭代器(包含尾迭代器)失效,或修改该范围的任何元素。
参数
| 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)
的要求。
|
||
-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++11 起有 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++11 起有 std::move ++first1; ++first2; } return init; } |
注意
此算法的可并行版本 std::transform_reduce 要求 op1 与 op2 可交换并可结合,但
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 | op1 和 op2 不能有任何副作用 | 它们不能修改涉及到的范围 |
| LWG 2055 (P0616R0) |
C++11 | acc 在累加时不会被移动 | 它会被移动 |
参阅
|
(C++17)
|
应用一个函数对象,然后以乱序规约 (函数模板) |
| 对一个范围内的元素求和 (函数模板) |
|
| 计算范围内元素的部分和 (函数模板) |