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

工艺品东莞网站建设shtml怎么做网站

工艺品东莞网站建设,shtml怎么做网站,wordpress拼图,什么是h5页面设计本文与reduce函数学习的知识深度很浅,主要是记录一下使用reduce来简化代码,高效解题。 参考资料: https://cppreference.cn/w/cpp/algorithm/reduce 1. reduce 函数概述 reduce 函数类似于 accumulate,但它允许更灵活的并行化和…

本文与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;
}

在这里插入图片描述

http://www.dtcms.com/wzjs/589877.html

相关文章:

  • 网站开发和软件开发的区别中国贸易网官网
  • 百度个人网站申请国际最新时事新闻热点
  • 做网站的背景怎么调整大小oppo应用市场
  • 保康县城乡建设路网站怎么下载在线视频
  • 苏州产品网站建设简述网络营销的特点及功能
  • 做网站容易还是编程容易app首页设计模板
  • jsp做网站的流程如何给wordpress添加一张网站背景
  • 网站备案部门企业网页申请制作步骤
  • 天津网站建设 泰姆仕北京网站建设 地址海淀
  • 设计师服务平台鱼巴士有哪些网站漯河知名网站建设价格
  • 大型物流公司网站软件开发内容有哪些
  • 广州网站优化外包如何申请域名创建一个网站
  • 网站系统管理员做一样的模板网站会被告侵权吗
  • 怎么做代刷网站教程用ps做商城网站好做吗
  • 新网站多久会被百度收录国外wordpress主题
  • 手把手做网站页面10秒折一把古风伞
  • 爱站网自媒体网站建设与维护采访稿
  • 做网站赚什么钱北京网站建设公司报价浩森宇特
  • 营销网站首页设计asp 公司网站源码
  • 长春网页制作建站英文wordpress换中文
  • 微网站建设公司首选济南浩辰网站建设公司怎么样
  • 女人和男人做爰网站做网站容易挣钱吗
  • 产品展示网站源码广州o2o网站建设
  • 软文广告示范jsp网站seo优化
  • 常州武进区建设局网站湖南常德广宇建设网站
  • 济南中建设计院有限公司网站学院网站建设项目
  • 网站备案ip地址段七台河新闻综合频道节目表
  • 郑州建设银行网站房贷网点在哪本地最新招聘信息
  • 买个网站服务器多少钱it公司排名
  • 网站开发 避免 字段变化 代码手机app定制多少钱