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

网站开发好学不网站外链建设与维护

网站开发好学不,网站外链建设与维护,校园门户网站系统建设方案,wordpress主题实现伪静态STL 算法分类: 类别常见算法作用排序sort、stable_sort、partial_sort、nth_element等排序搜索find、find_if、count、count_if、binary_search等查找元素修改copy、replace、replace_if、swap、fill等修改容器内容删除remove、remove_if、unique等删除元素归约for…

STL 算法分类:

类别常见算法作用
排序sortstable_sortpartial_sortnth_element排序
搜索findfind_ifcountcount_ifbinary_search查找元素
修改copyreplacereplace_ifswapfill修改容器内容
删除removeremove_ifunique删除元素
归约for_eachaccumulate处理数据
合并mergeset_unionset_intersection处理有序序列
排列组合next_permutationprev_permutation生成排列
堆操作push_heappop_heapmake_heapsort_heap处理堆

STL 归约算法

STL 中的归约算法(Reduction Algorithms)主要用于从一个容器或范围中计算一个单一的结果,例如对所有元素进行累加、求最小值、求最大值等。
归约算法常见的包括 accumulateinner_productpartial_sum 等,它们通常涉及到对容器中元素的聚合、计算或比较。

算法名称功能描述时间复杂度空间复杂度适用场景
accumulate累加容器中的元素,支持自定义操作O(n)O(1)对容器进行累加或其他二元操作
reduce并行化版本的accumulate(C++17)O(n)O(1)accumulate 类似
for_each对范围内每个元素执行一个函数O(n)O(1)对容器中的每个元素执行某种操作
max_element返回容器中最大元素的迭代器O(n)O(1)查找容器中的最大元素
min_element返回容器中最小元素的迭代器O(n)O(1)查找容器中的最小元素
inner_product计算两个容器元素的内积或加权和O(n)O(1)计算两个向量的点积或加权和
partial_sum计算部分和,将容器中的每个元素累加到结果容器中O(n)O(n)计算前缀和或部分和
adjacent_difference计算相邻元素的差值,并将结果存储到新容器中O(n)O(n)计算相邻元素的差值

(1)accumulate

  • 功能:从容器的起始位置开始,对容器中的每个元素进行累加,得到最终的结果。它还支持自定义的二元操作(比如乘法、最大值等)。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(1),原地操作。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <numeric>  // accumulateint main() {vector<int> vec = { 1, 2, 3, 4, 5 };// 累加所有元素的和int sum = accumulate(vec.begin(), vec.end(), 0);  // 初始值为0cout << "Sum: " << sum << endl;  // 输出:Sum: 15// 使用自定义操作(乘法)int product = accumulate(vec.begin(), vec.end(), 1, multiplies<int>());cout << "Product: " << product << endl;  // 输出:Product: 120system("pause");return 0;
}

注意:

  • accumulate适用于,当需要对容器中的元素进行累加或执行其他二元操作时。

(2)reduce(C++17)

  • 作用:与 accumulate 类似,但允许并行计算(并行化需与 <execution> 配合)。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(1),原地操作。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <numeric>  // reduceint main() {vector<int> v = { 1, 2, 3, 4 };int sum = reduce(v.begin(), v.end()); // 累加和cout << "Sum with reduce: " << sum << endl;system("pause");return 0;
}

注意

  • reduce在普通使用下与 accumulate 类似。

(3)for_each

  • 作用:对每个元素执行某个操作。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(1),原地操作。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>  // for_eachint main() {vector<int> v = { 1, 2, 3 };for_each(v.begin(), v.end(), [](int& x) { x *= 2;});for (int val : v){cout << val << " ";  // Output: 2 4 6}cout << endl;system("pause");return 0;
}

注意:

  • [](int& x) { x *= 2; }一个lambda表达式,它接受一个引用参数x,并将x的值乘以2。

(4)max_element

  • 功能:返回容器中最大元素的迭代器。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(1),原地操作。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm> // max_elementint main() {vector<int> vec = { 1, 3, 2, 4, 5 };auto max_iter = max_element(vec.begin(), vec.end());cout << "最大的元素: " << *max_iter << endl;  // 输出:5system("pause");return 0;
}

注意:

  • max_element适用于,当需要找到容器中的最大元素时。

(5)min_element

  • 功能:返回容器中最小元素的迭代器。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(1),原地操作。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm> // min_elementint main() {vector<int> vec = { 1, 3, 2, 4, 5 };auto min_iter = min_element(vec.begin(), vec.end());cout << "最小的元素: " << *min_iter << endl;  // 输出:1system("pause");return 0;
}

注意:

  • min_element适用于,当需要找到容器中的最小元素时。

(6)inner_product

  • 功能:计算两个容器中元素的内积,也可以计算加权和。它接受两个范围作为输入,并执行加法和乘法。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(1),原地操作。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <numeric>  // inner_productint main() {vector<int> vec1 = { 1, 2, 3 };vector<int> vec2 = { 4, 5, 6 };// 计算内积:1*4 + 2*5 + 3*6int result = inner_product(vec1.begin(), vec1.end(), vec2.begin(), 0);cout << "内积: " << result << endl;  // 输出:32system("pause");return 0;
}

注意:

  • inner_product常用于计算两个向量的点积或加权和。

(7)partial_sum

  • 功能:计算一个容器的部分和,即将容器中的每个元素累加起来,返回一个包含部分和的容器。可以选择自定义操作(例如累乘)。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(n),需要存储部分和的结果。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <numeric>  // partial_sumint main() {vector<int> vec = { 1, 2, 3, 4, 5 };vector<int> result(vec.size());// 计算部分和partial_sum(vec.begin(), vec.end(), result.begin());for (int x : result){cout << x << " ";  // 输出:1 3 6 10 15}cout << endl;system("pause");return 0;
}

注意:

  • partial_sum用于计算前缀和或部分和。

(8)adjacent_difference

  • 功能:计算容器中相邻元素的差值,返回一个新的容器,其中每个元素是相邻元素的差值。
  • 时间复杂度O(n),其中 n 是容器的元素数量。
  • 空间复杂度O(n),需要存储差值结果。

示例:

#include <iostream>
using namespace std;
#include <vector>
#include <numeric>  // adjacent_differenceint main() {vector<int> vec = { 1, 3, 6, 10, 15 };vector<int> result(vec.size());// 计算相邻元素的差值adjacent_difference(vec.begin(), vec.end(), result.begin());for (int x : result){cout << x << " ";  // 输出:1 2 3 4 5}cout << endl;system("pause");return 0;
}

注意:

  • adjacent_difference用于计算相邻元素的差值,常用于处理一系列增量或差异。

文章转载自:

http://S4oZCvy5.dbnpz.cn
http://QQcj1kuh.dbnpz.cn
http://us9UwMmG.dbnpz.cn
http://GhyoxPts.dbnpz.cn
http://rqMXKH3D.dbnpz.cn
http://e7xjlAce.dbnpz.cn
http://dwdODfY8.dbnpz.cn
http://fNKRHQ0q.dbnpz.cn
http://fRFjFY49.dbnpz.cn
http://4jJxwjDN.dbnpz.cn
http://sS5eEwfH.dbnpz.cn
http://9k8YEO56.dbnpz.cn
http://ygIrFOtH.dbnpz.cn
http://HwWfgmUd.dbnpz.cn
http://Roey17Iu.dbnpz.cn
http://ArKlLXhL.dbnpz.cn
http://TtTNQEFJ.dbnpz.cn
http://4wMbCHj3.dbnpz.cn
http://1esAA1M5.dbnpz.cn
http://4RKLUhyM.dbnpz.cn
http://0TxxktCD.dbnpz.cn
http://ZtRV2kAU.dbnpz.cn
http://eVsaDGRL.dbnpz.cn
http://P1jrkKsQ.dbnpz.cn
http://wFhzRtLS.dbnpz.cn
http://eqV4owJT.dbnpz.cn
http://zORnPL1g.dbnpz.cn
http://2NjMFXrA.dbnpz.cn
http://zT6to7nS.dbnpz.cn
http://LurunFsc.dbnpz.cn
http://www.dtcms.com/wzjs/631967.html

相关文章:

  • 浙江台州网络设计网站汶上网站制作
  • 网站上线前做环境部署无法使用wordpress
  • 中国建设监理协会网站个人会员系统自己做网站自己买服务器
  • 做网站有必要虚拟主机网站建设过程
  • 购物手机网站怎么做网站建设费和网站维护费的区别
  • h5互动网站建设做电商网站需要多少时间
  • 专业网站定制价格网站开发 招标采购参数
  • 网站开发入哪个会计科目wordpress前台注册登陆
  • 有了网站开发app是不是更容易技术支持 东莞网站建设洋酒回收
  • .net网站开发实训wordpress分享卡片插件
  • 国外网站页面做多大个人直播网站怎么做
  • 网站建设公司创业广州网站公司制作网站
  • 建设婚恋网站基本功能有哪些东城东莞网站建设
  • 视频网站备案广告资源发布平台
  • 西安商城网站建设咪豆苏中建设集团官方网站
  • 织梦后台点击网站主页网站地图深度做多少合适
  • 恩施网站优化理发美发培训学校
  • 上海网站建设觉策网站建设账户搭建
  • 英国电商网站wordpress 自定义主题
  • 宁波建网站公司深圳市做网站的公司
  • 做公司网站的公司有哪些做网站大公司
  • 外贸网站建设公司青岛wordpress商品采集器
  • 新站加快网站收录青海网站开发
  • 企业网站flash学校网站策划书
  • 网站开发与管理专业静宁县建设局网站
  • 娄底建设网站百分百微信营销软件
  • 做网站用哪些软件网站备案要啥
  • 网站幕布拍照什么样子的电子邀请函制作免费模板
  • 运城网站建设多少钱平面设计广告
  • 苏州建设网站专业东营房产信息网