C++ std::inner_product 把两个区间对应元素相乘再累加到初值上,即点积。
原型
T inner_product(InputIt1 first1, InputIt1 last1, InputIt2 first2, T init);参数
- 第一个区间
[first1, last1)。 first2:第二个区间起点,长度按第一区间计。init:初值。
返回值
内积。
示例
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> a{1, 2, 3};
std::vector<int> b{4, 5, 6};
std::cout << std::inner_product(a.begin(), a.end(), b.begin(), 0) << "\n";
return 0;
}输出:
32
注意事项
1*4 + 2*5 + 3*6 = 32。第二区间必须至少与第一区间一样长。
本头文件到此结束。可返回numeric 总览或进入utility。