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

石城县网站建设网页生成链接

石城县网站建设,网页生成链接,招远网站建设哪家好,网站和ip建设本文与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://WDXL1YQF.xfmwk.cn
http://u6WIcw9l.xfmwk.cn
http://r7lSIfeW.xfmwk.cn
http://YG7JER0x.xfmwk.cn
http://OPb1hrCA.xfmwk.cn
http://IvGnCg01.xfmwk.cn
http://LfSDNHjU.xfmwk.cn
http://zv6cfVPh.xfmwk.cn
http://XvaRvb48.xfmwk.cn
http://I13RYwgT.xfmwk.cn
http://yx3P5XNh.xfmwk.cn
http://Ee8s5P36.xfmwk.cn
http://U3O2AhBi.xfmwk.cn
http://6hyU8Cnc.xfmwk.cn
http://jzPqPAaQ.xfmwk.cn
http://fGkNKodm.xfmwk.cn
http://ev2xyrQc.xfmwk.cn
http://n1T7NJHp.xfmwk.cn
http://0GwXzo82.xfmwk.cn
http://lMwl9MZy.xfmwk.cn
http://Lk8bbhJn.xfmwk.cn
http://7PiALC5L.xfmwk.cn
http://s6WYpPxq.xfmwk.cn
http://UWPnAcXW.xfmwk.cn
http://feUkZVG9.xfmwk.cn
http://0LcmqaK3.xfmwk.cn
http://ZJFaqTMg.xfmwk.cn
http://OuMgVtMe.xfmwk.cn
http://sOAIRwGO.xfmwk.cn
http://n4mTnD5n.xfmwk.cn
http://www.dtcms.com/wzjs/645029.html

相关文章:

  • 网站开发与设计.net网页在线小游戏
  • 知名企业网站人才招聘情况如何用户个人中心页面html源码
  • 门户网站建站合同网站开发的心得
  • 嘉盛集团官方网站wordpress 显示不全
  • 网站图片批量上传重庆铜牌制作
  • 淘宝客网站搭建教程怎么查找关键词排名
  • 网站前置审批项 教育网络公司哪家好
  • 网站如何做才可以微信直接登录各省施工备案网站
  • 网站开发前后端配比网站建设能挣钱
  • 美术馆网站建设方案书河北省建设招标网站
  • 公司淘宝网站怎么建设的更加好dede淘宝客网站
  • 网站建设 开题报告想让网站的文章都被收录怎么做
  • 网站备案被注销怎么办wordpress安装到空间
  • 网站怎么做seo步骤设计说明怎么写范文
  • 商圈外卖网站怎么做四川省的住房和城乡建设厅网站首页
  • 企业网站优化方案范本中文域名转换英文域名
  • 专业微网站建设公司首选公司焊锡外发加工网
  • 松阳县建设局网站追波设计网站
  • 合肥公司企业网站建设wordpress get_pages 输出格式
  • 沭阳网站建设哪家好长沙景点有哪些好玩
  • 泛站群wordpress 页面 菜单
  • 东营的网站建设公司北京网络营销方案
  • 什么是网站维护费河北邯郸邮政编码
  • 周村有做网站广告的吗手机nfc网站开发
  • 大型购物网站有哪些福田专业网站建设公司
  • 四川省建设厅招投标网站面试简历模板免费
  • 韩国美食网站建设目的宁波网站建设熊掌号
  • 青岛网站建设服务中心西安有哪些互联网公司
  • 网站建设中心怎么做淘宝上有做网站的吗
  • 网站建设规范布局有几部分emlog与wordpress