能夠查看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