能够查看https://zh.cppreference.com/w...python
注意: 必需要保证目标位置有足够的空间(resize 或者 reserve),标准算法在大多数状况下不能检查目标范围是否足够大c++
copy(@beg, @end, @target_begin) -> @target_out copy_n(@beg, n, @target_begin) -> @target_out copy_backward(@beg, @end, @target_end) -> @target_begin (拷贝到end处) copy_if(@beg, @end, @target,f(O)->bool)->target_end sample(@beg, @end, @out, n, random_generator) ->out_end (实现采样-c++17)
#shitf Elements 转换元素 1.reserve/reserve_copy 反转 2.rotate/rotate_copy 旋转 shift_left/shift_right(X感受用处不大c++20) 3.shuffle(@beg, @end, randim_engine) 随机打乱 #sort 排序 1.sort(@begin, @end, compare(o,o)->bool) 2.stable_sort(@begin, @end, compare(o,o)->bool) //a.sort是快速排序实现,所以是不稳定的;stable_sort是归并排序实现,所以是稳定的; //b.对于相等的元素sort可能改变顺序,stable_sort保证排序后相等的元素次序不变 //c.若是提供了比较函数,sort不要求比较函数的参数被限定为const,而stable_sort则要求参数被限定为const,不然编译不能经过 3.partial_sort(@begin, n, @end, compare(o,o)->bool) /partial_sort_copy() nth_elements(没想到用途) 4.is_sorted(@begin, @end, compare(o,o)->bool) -> true (是否有序) 5.is_sorted_until(@begin, @end, compare(o,o)->bool) -> @sorted_end (返回到哪里以前是有序的) # partition 分区 1.partition(@beg, @end, f(o,o)->bool) ->@ftrue_end 2.partition_copy(@beg, @end, @ft, @ff, f(O)->bool) -> {@ft_end, @ff_end} 3.stable_partition(@beg, @end, f(o,o)->bool) ->@ftrue_end // stable_partition保证分区后原来的前后顺序不变,而partition没法保证 4.is_partition() -> true 5.partition_point() -> @ftrue_end (分区后返回分界点) # Permutations 排列组合(不知道应用场景??) 1.next_permutation(@beg, @end) -> true 只要下一种排列能够是逻辑上大的,就返回true 2.prev_permutataion(@beg, @end) -> true 只要上一种排列能够是逻辑上小的,就返回true 3.is_permutation(@beg, @end, @beg2) -> true if range2 is a permutation of range1
# Filling / Overwriting Ranges 填充改写 1.fill(@beg, @end, value) ->@filled_end (fill_n) 2.generate(@begin ,@end generator()-> ●) (generator_n) //generator能够经过functors 写入不一样的value,比fill功能强,不用循环这么捞的写了 # Changing / Replacing Values 改变替代 1.transform(@beg, @end, @out, f(O)->■) -> @out_end 2.transform(@beg, @end, @beg2, @out, f(O,△) -> ■) -> @out_end //该算法在其余编程语言中也称为"map" //target 必须可以接受和input range元素同样多的元素 //f必须没有 side_effect / be stateful , 由于不能保证将functors应用于输入元素的顺序 3.replace(@beg, @end, old_value, new_value) / replace_if(@beg, @end, condition(O)->bool) 4.replace_copy/replace_copy_if
1.remove/remove_if(@beg, @end, f(O)->bool) -> @remaining_end 2.remove_copy/remove_copy_if(@beg, @end, @out, f(O)->bool)->@oyt_end 3.unique/unique_copy(@begin, @end, @out) -> @remaining_end (相似去重??) //remove/unique等操做 仅将其他元素移动到输入范围的前面,并不调整容器的大小或分配内存 //若是想要修改容器,resize/shrink it ,而后使用容器的erase or resize 成员函数(c++20起有独立的erase算法可使用)
#include <numeric> //type1: Reductions(归约⊕) - produce one result based on a sequence of input elements //type2: Produce a sequence of results with the same number of elements as the input sequence 1.iota(@beg, @end, start_value = 1) //相似 python range() ,c++11 # Reductions(归约⊕) 1.reduce(@beg, @end, w, ⊕(□+○)->■) 相似accumulate增强版,c++17 eg. reduce(begin(w), end(w), 1.0, std::multiplies<>{}); 2.transform_reduce( @beg, @end, w, ⊕(□+○)->■, f(O)->▲)->Rf ,c++17 eg. transform_reduce(begin(v), end(v), 1, std::plus<>{}, f); 3.老的操做accumulate(@b, @e, w, ⊕(□,○))不能并行 # Span(扫描) 1.adjacent_difference(@b, @e, @o); //计算范围内各相邻元素之间的(差)或者(f,c++17起能够加上custom operator) 2.inclusive_scan(@b, @e, @o, f); //c++17, 相似std::partial_sum,第i个和中包含第i个输入 3.exclusive_scan(@b, @e, @o, w, f);//c++17,相似std::partial_sum,第i个和中排除第i个输入 4.transform_inclusive_scan(@b, @e, @o, ⊕, f, w);//c++17 应用一个函数对象,而后进行包含扫描 5.transform_inclusive_scan(@b, @e, @o, ⊕, f, w);//c++17,应用一个函数对象,而后进行排除扫描 6.老版partial_sum(@b, @e, @o, f);
须要程序员保证已排序状态(is_sorted),算法并不会检查程序员
# Binary Searches 二分查,O(log N) steps 最坏 O(N) steps 1.binary_search(@b, @e, value)->true //肯定元素是否存在于某范围中 2.lower_bound(@b, @e, value)->@1st_element//@end //返回指向第一个不小于给定值的元素的迭代器[] 2.upper_bound(@b, @e, value)->@1st_element //返回指向第一个大于给定值的元素的迭代器[) 3.equal_range(@b, @e, value)->@1st_element //返回匹配特定键值的元素范围 4.include(@b1, @e1, @b2, @e2)->true # Merging 归并,能够在线性时间内将两个排序的序列合并为一个排序的序列。 1.merge(@b1, @e1, @b2, @e2, @o)->@o_end 2.inplace_merge(@first @second @end, compare(o,o)->bool) # Set Operations 集合操做, 能够在线性时间内完成,比没排序的要快 1.set_union(@b1, @e1, @b2, @e2, @o,compare(o,o)->bool)->@o_end //计算两个集合的并集 2.set_intersection(@b1, @e1, @b2, @e2, @o,compare(o,o)->bool)->@o_end//计算两个集合的交集 3.set_diffrence(@b1, @e1, @b2, @e2, @o,compare(o,o)->bool)->@o_end//计算两个集合的差集 4.set_symmetric_difference(@b1, @e1, @b2, @e2, @o,compare(o,o)->bool)->@o_end//计算两个集合的对称差
# Initialize Heap 初始化堆 1.make_heap(@b, @e, compare(O,O)->bool/*默认<大顶堆*/); # Shrink Heap 堆收缩 1.pop_heap(@b, @e, compare(O,O)->bool); # Grow Heap 堆成长 1.push_heap(@b, @e, compare(O,O)->bool); # sort Heap (Heap -> Sorted Array) 1.sort_heap(@b, @e, compare(O,O)->bool); # is Heap 1.is_heap(@b, @e, compare(O,O)->bool)->true 2.is_heap_until(@b, @e, compare(O,O)->bool)->heap_end
主要能够塞不一样的元素,通常的容器只能容纳相同的东西(若是不一样通常用对象指针的方式来塞)算法
随机数生成 = distribution(分布) + engine(随机数生成器)编程
#include<random> auto urng = std::mt19937{}; //Mersenne Twister(梅森旋转), a good default urng.seed(123);// sed engine with constant ,not necessary auto distr = std::uniform_real_distribution<float>{-1.2f, 6.25f}; cout << distr(urng) <<std::endl; auto dice = [&] {return distr(urng) + 1}; //Cumtom Generators