理解 C++ 中的 std::transform_reduce 及示例
std::transform_reduce 是一个强大的 C++17 算法,它结合了 transform 和 reduce(或 accumulate)的功能。它允许你对元素进行转换,然后使用二元操作进行归约,从而写出简洁高效的代码。
语法
template<class InputIt1, class InputIt2, class T,
class BinaryOp1, class BinaryOp2>
T transform_reduce(InputIt1 first1, InputIt1 last1,
InputIt2 first2, T init,
BinaryOp1 binary_op1,
BinaryOp2 binary_op2);
template<class InputIt, class T,
class BinaryOp1, class UnaryOp>
T transform_reduce(InputIt first, InputIt last,
T init,
BinaryOp1 binary_op1,
UnaryOp unary_op);
- 它可以对每个元素应用 一元转换(可选)。
- 然后使用 二元操作对结果进行归约,如求和、求积或自定义组合。
- 在 C++17/20 中支持 并行执行策略。
示例 1:求平方和
#include <iostream>
#include <vector>
#include <numeric>
#include <execution>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
int sum_of_squares = std::transform_reduce(
std::execution::seq, // 顺序执行
numbers.begin(),
numbers.end(),
0, // 初始值
std::plus<>(), // 二元操作(求和)
[](int x){ return x*x; } // 一元转换(平方)
);
std::cout << "平方和: " << sum_of_squares << std::endl;
return 0;
}
示例 2:向量点积
#include <iostream>
#include <vector>
#include <numeric>
int main() {
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {4, 5, 6};
int dot_product = std::transform_reduce(
a.begin(), a.end(),
b.begin(),
0 // 初始值
);
std::cout << "点积: " << dot_product << std::endl;
return 0;
}
示例 3:并行 transform_reduce
#include <iostream>
#include <vector>
#include <numeric>
#include <execution>
int main() {
std::vector<double> numbers(1'000'000, 1.5);
double sum = std::transform_reduce(
std::execution::par, // 并行执行
numbers.begin(),
numbers.end(),
0.0
);
std::cout << "并行求和: " << sum << std::endl;
return 0;
}
关键点
transform_reduce避免了为转换后的值创建中间容器。- 支持 顺序 和 并行执行策略。
- 有两种主要形式:单个范围带一元转换,或者两个范围进行成对操作(如点积)。
- 初始值是必须的,以正确处理空范围。
std::transform_reduce 可以让你的代码更简洁、更高效,尤其适合大数据集或并行计算场景。
C/C++编程
- 理解C++中的std::transform_reduce及示例
- 使用原子 TAS 指令实现自旋锁
- C++中检测编译时与运行时: if consteval 与 std::is_constant_evaluated()
- C++ 转发引用: 完美转发的关键
- 理解 C++ 中的 dynamic_cast: 安全的向下转型与向上转型
- C与C++: restrict关键字及其在编译器优化中的作用
- C++的左值/lvalue, 右值/rvalue和右值引用/rvalue references
- C++中的assert和static_assert的区别
- C++: auto_ptr智能指针被弃用
- C++中的consteval是什么? 它与const和constexpr有何不同?
- C++ 教程: 用std::move来移动所有权
- C++中的 const和constexpr 比较
- 简易教程: C++的智能指针
- C++ 编程练习题: 如何合并两个二叉树?
- C++ 编程练习题 - 找出第三大的数
- C++ 编程练习题 - 最多连续的 1
- C++ 编程练习题 - 左子树叶节点之和 (深度优先+广度优先+递归)
- C++ 编程练习题 - 最多水容器 (递归)
- C++的异步编程: std::future, std::async 和 std::promise
- C编程练习题: 翻转整数位
- C++编程练习题: 找出字符串的所有大小小组合
- C/C++ 中的内存管理器(堆与栈)
- C++编程练习题: 对两单向链表求和
英文:Understanding std::transform_reduce in Modern C++
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK