当前位置: 首页 > news >正文

C++ 中的reduce函数使用指南

本文与reduce函数学习的知识深度很浅,主要是记录一下使用reduce来简化代码,高效解题。

参考资料:
https://cppreference.cn/w/cpp/algorithm/reduce


1. reduce 函数概述

reduce 函数类似于 accumulate,但它允许更灵活的并行化和元素分组。头文件是 <numeric>

函数原型中的几种重载形式:

#include <numeric>
#include <iostream>

// 没有初始值
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 BinaryOp >
T reduce( InputIt first, InputIt last, T init, BinaryOp op );

2. 简单案例

简单求和

#include <iostream>
#include <vector>
#include <numeric>
int main() {
    vector<int> numbers = {1, 2, 3, 4, 5};
    int sum = reduce(numbers.begin(), numbers.end());
    cout << "Sum : " << sum << endl;
    return 0;
}

自定义操作

reduce 函数还可以使用自定义的二元操作函数。下面计算数组中所有元素的乘积:

#include <iostream>
#include <vector>
#include <numeric>
int main() {
    vector<int> numbers = {1, 2, 3, 4, 5};
    int ret= reduce(numbers.begin(), numbers.end(), 1, [](int a, int b) {
        return a * b;
    });
    cout << "ret: " << ret<< endl;
    return 0;
}

并行化

从 C++17 开始,reduce 支持并行化执行,加速计算。

#include <iostream>
#include <vector>
#include <numeric>
#include <execution>
int main() {
    vector<int> numbers = {1, 2, 3, 4, 5};
    // 使用并行策略
    int sum = reduce(std::execution::par, numbers.begin(), numbers.end());
    cout << "Sum (parallel): " << sum << endl;
    return 0;
}

3. 简单练习

使用 reduce 函数计算以所有元素的平方和:

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    int sum_of_squares = reduce(numbers.begin(), numbers.end(), 0, [](int a, int b) {
        return a + b * b;
    });
    std::cout << "Sum of squares: " << sum_of_squares << std::endl;
    return 0;
}

使用 reduce 函数和并行策略计算以下数组中所有元素的最大值:

std::vector<int> numbers = {1, 3, 91, 12, 4};
#include <iostream>
#include <vector>
#include <numeric>
#include <execution>

int main() {
    std::vector<int> numbers = {10, 20, 30, 40, 50};
    int max_value = reduce(std::execution::par, numbers.begin(), numbers.end(), numbers[0], [](int a, int b) {
        return a > b ? a : b;
    });
    std::cout << "Maximum value: " << max_value << std::endl;
    return 0;
}

在这里插入图片描述

相关文章:

  • AI数据分析:用DeepSeek做数据清洗
  • Ubuntu系统上部署Node.js项目的完整流程
  • MySQL数据库入门到大蛇尚硅谷宋红康老师笔记 高级篇 part 5
  • 代码随想录算法【Day57】
  • 深入浅出:插入排序算法完全解析
  • 事业编体检不合格有哪些?
  • 重新审视 ChatGPT 和 Elasticsearch:第 2 部分 - UI 保持不变
  • MotionLM技术路线与优势解析
  • 【Oracle专栏】sqlplus显示设置+脚本常用显示命令
  • Rust ~ Vec<u8>和[u8]
  • Redis源码剖析之GEO——Redis是如何高效检索地理位置的?
  • Nginx+PHP+MYSQL-Ubuntu在线安装
  • Qt开发⑨Qt的事件_事件处理_按键事件和鼠标事件
  • 如何查找APP漏洞并渗透测试 解决网站被黑客攻击
  • BufferedReader PrintWriter
  • ctfhub-web信息泄露通关攻略
  • LabVIEW图像识别抗干扰分析
  • STM32学习【4】ARM汇编(够用)
  • 【Java项目】基于Spring Boot的校园闲置物品交易网站
  • IP 地址分配和管理全解析
  • 微软宣布将裁员3%
  • 商务部召开外贸企业圆桌会:全力为外贸企业纾困解难,提供更多支持
  • 左娅︱悼陈昊
  • 复旦相辉堂上演原创历史人物剧《王淑贞》,胡歌参演
  • 黄土是他们的气质:打破宁夏当代油画创作的沉寂
  • 江西省直机关工委副书记熊亮华履新宜春市委常委、宣传部部长