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

快速建设企业网站深圳网站设计知名乐云seo

快速建设企业网站,深圳网站设计知名乐云seo,安卓软件开发公司收入,网站集群建设价格本文与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/170449.html

相关文章:

  • 进口外贸网站有哪些百度竞价可以自学吗
  • 求和萝莉做的网站铜仁搜狗推广
  • 网站的建设外链优化网络推广公司排行榜
  • 企业模式网站列表管理器广州网站优化运营
  • 做兼职的网站有哪些什么是搜索引擎优化?
  • 泰安住房和城乡建设厅网站免费推广公司的网站
  • 网站开发是用模版还是纯手打太原百度推广排名优化
  • 友汇网 做公司网站网络推广员是干嘛的
  • 同步网站内容怎么做seo中文意思
  • 单页网站建设哪个品牌好做公司网站
  • java做的网站影响seo排名的因素有哪些
  • 如何做网站微信支付网站搜索排名优化价格
  • 网站开发的论文怎么写营销策略ppt模板
  • 北京文化传媒有限公司网站建设阿里云域名
  • 人才微网站开发seo站长综合查询
  • 美德的网站建设连云港网站seo
  • wordpress 最后一页朝阳seo搜索引擎
  • 中小企业网站建设多少钱百度seo搜索引擎优化培训
  • 做一个小程序seo营销的概念
  • 自助建设手机网站网络营销乐云seo
  • 郴州网站建设设计制作营销推广方案案例
  • 汇川区住房和城乡建设厅网站网络营销的真实案例分析
  • 设计师网站behanceseo培训学校
  • 网站建设可用性的五个方面西安搜建站科技网站
  • 天圆地方建筑网站引擎优化seo
  • 如何设计一个网页并举例说明郑州seo公司哪家好
  • 沃通 wordpressseo每天一贴博客
  • 网站在线报名怎么做外贸推广平台有哪几个
  • 南京的电商网站设计网页设计培训
  • 成都响应式网站建设网络推广人员