C++17 中的 std::reduce:详细教程

文章目录

    • [1. 简介](#1. 简介)
    • [2. 函数签名](#2. 函数签名)
    • [3. 使用场景](#3. 使用场景)
      • [3.1 简单的累加操作](#3.1 简单的累加操作)
      • [3.2 自定义归并操作](#3.2 自定义归并操作)
      • [3.3 并行计算的性能优势](#3.3 并行计算的性能优势)
    • [4. 注意事项](#4. 注意事项)
      • [4.1 归并操作的结合律和交换律](#4.1 归并操作的结合律和交换律)
      • [4.2 默认值的使用](#4.2 默认值的使用)
    • [5. 总结](#5. 总结)

1. 简介

std::reduce 是 C++17 标准库中引入的一个算法,用于对范围内的元素进行归并操作。它类似于 std::accumulate,但在某些情况下提供了更灵活的处理方式,尤其是在并行计算方面。

2. 函数签名

std::reduce 的基本函数签名如下:

cpp 复制代码
template<class InputIt>
typename std::iterator_traits<InputIt>::value_type
reduce(InputIt first, InputIt last);

template<class InputIt, class T>
T reduce(InputIt first, InputIt last, T init);

template<class InputIt, class T, class BinaryOperation>
T reduce(InputIt first, InputIt last, T init, BinaryOperation binary_op);

template<class ExecutionPolicy, class ForwardIt>
typename std::iterator_traits<ForwardIt>::value_type
reduce(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last);

template<class ExecutionPolicy, class ForwardIt, class T>
T reduce(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, T init);

template<class ExecutionPolicy, class ForwardIt, class T, class BinaryOperation>
T reduce(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, T init, BinaryOperation binary_op);
  • InputIt/ForwardIt:迭代器类型,表示要归并的范围。
  • T:归并操作的初始值类型。
  • BinaryOperation:用于归并的二元操作函数。
  • ExecutionPolicy:执行策略,可以是 std::execution::seq(顺序执行)、std::execution::par(并行执行)或 std::execution::unseq(无序执行)。

3. 使用场景

3.1 简单的累加操作

以下是一个简单的累加示例:

cpp 复制代码
#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};
    int sum = std::reduce(nums.begin(), nums.end());
    std::cout << "Sum: " << sum << std::endl; // 输出 15
    return 0;
}

这里,std::reduce 默认使用加法操作。

3.2 自定义归并操作

可以通过传递自定义的二元操作函数来实现不同的归并逻辑。例如,计算数组中元素的最大值:

cpp 复制代码
#include <iostream>
#include <vector>
#include <numeric>
#include <execution>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};
    int maxElement = std::reduce(std::execution::par, nums.begin(), nums.end(), nums[0], [](int a, int b) {
        return std::max(a, b);
    });
    std::cout << "Maximum element: " << maxElement << std::endl; // 输出 5
    return 0;
}

这里,我们使用了 std::execution::par 来启用并行执行。

3.3 并行计算的性能优势

std::reduce 支持并行执行策略,这使得它在处理大规模数据时能够显著提高性能。例如,计算一个大数组的和:

cpp 复制代码
#include <iostream>
#include <vector>
#include <numeric>
#include <execution>
#include <chrono>

int main() {
    std::vector<int> nums(10000000, 1); // 一个包含 1000 万个元素的数组
    auto start = std::chrono::high_resolution_clock::now();
    int sum = std::reduce(std::execution::par, nums.begin(), nums.end());
    auto end = std::chrono::high_resolution_clock::now();
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    std::cout << "Sum: " << sum << ", Time taken: " << duration << " ms" << std::endl;
    return 0;
}

通过使用 std::execution::par,std::reduce 可以利用多核处理器进行并行计算。

4. 注意事项

4.1 归并操作的结合律和交换律

std::reduce 的归并操作要求是结合律(Associative)和交换律(Commutative)的。如果归并操作不满足这些性质,结果可能是不确定的。例如,减法操作不满足结合律和交换律,因此在并行执行时可能会导致不同的结果:

cpp 复制代码
std::vector<int> nums = {32, 16, 8, 4, 2, 1};
int result1 = std::reduce(nums.begin() + 1, nums.end(), nums[0], std::minus<>{});
int result2 = std::reduce(std::execution::par, nums.begin() + 1, nums.end(), nums[0], std::minus<>{});
std::cout << result1 << ", " << result2 << std::endl; // 输出可能不同

4.2 默认值的使用

std::reduce 的默认值是元素类型的默认构造值。如果默认值不是归并操作的单位元(Identity Element),可能会导致意外的结果。例如,对于整数类型,加法的单位元是 0,乘法的单位元是 1。

5. 总结

std::reduce 是一个强大且灵活的算法,适用于各种归并操作,尤其是需要并行处理的场景。它与 std::accumulate 类似,但在并行执行方面提供了更好的支持。通过合理使用 std::reduce,可以简化代码并提高性能。

希望这篇教程对你有所帮助!如果有任何问题,欢迎随时提问。

相关推荐
海边的Kurisu4 分钟前
苍穹外卖日记 | Day1 苍穹外卖概述、开发环境搭建、接口文档
java
C雨后彩虹4 小时前
任务最优调度
java·数据结构·算法·华为·面试
heartbeat..4 小时前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
Jing_jing_X4 小时前
AI分析不同阶层思维 二:Spring 的事务在什么情况下会失效?
java·spring·架构·提升·薪资
SmartRadio5 小时前
CH585M+MK8000、DW1000 (UWB)+W25Q16的低功耗室内定位设计
c语言·开发语言·uwb
rfidunion5 小时前
QT5.7.0编译移植
开发语言·qt
rit84324995 小时前
MATLAB对组合巴克码抗干扰仿真的实现方案
开发语言·matlab
元Y亨H6 小时前
Nacos - 服务发现
java·微服务
微露清风6 小时前
系统性学习C++-第十八讲-封装红黑树实现myset与mymap
java·c++·学习
dasi02276 小时前
Java趣闻
java