赞
踩
计算两个向量的内积,它是 std::inner_product 的泛化版本,支持lambda表达式自定义运算。
- #include <iostream>
- #include <vector>
- #include <numeric>
- #include <execution>
-
- int main() {
- std::vector<int> data{1, 2, 3, 4, 5};
- //lambda 自定义运算
- auto sum_of_squares = std::transform_reduce(data.begin(), data.end(),
- 0, std::plus<>{}, [](int v) { return v*v; });
- std::cout << "sum_of_squares == " << sum_of_squares << "\n";
- //sum_of_squares = 55
-
- std::vector<int> coef{1, -1, 1, -1, 1};
- auto result = std::transform_reduce(data.begin(), data.end(),
- coef.begin(), 0);
- std::cout << "result == " << result << "\n";
- //result == 1*1 + 2*(-1) + 3*1 + 4*(-1) + 5*1 == 3
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。