算法部分主要由頭文件<algorithm>,<numeric>和<functional>組成。
<algorithm>是全部STL頭文件中最大的一個,其中經常使用到的功能範圍涉及到比較、 交換、查找、遍歷操做、複製、修改、反轉、排序、合併等等。
<numeric>體積很小,只包括幾個在序列上面進行簡單數學運算的模板函數,包括加 法和乘法在序列上的一些操做。
<functional>中則定義了一些模板類,用以聲明函數對象。
STL提供了大量實現算法的模版函數,只要咱們熟悉了STL以後,許多代碼能夠被大 大的化簡,只須要經過調用一兩個算法模板,就能夠完成所須要的功能,從而大大地提高效率。
#include <algorithm>
#include <numeric>
#include <functional> 算法
操做對象
直接改變容器的內容
將原容器的內容複製一份,修改其副本,而後傳回該副本
功能:
非可變序列算法 指不直接修改其所操做的容器內容的算法
計數算法 count、count_if
搜索算法 search、find、find_if、find_first_of、…
比較算法 equal、mismatch、lexicographical_compare
可變序列算法 指能夠修改它們所操做的容器內容的算法
刪除算法 remove、remove_if、remove_copy、…
修改算法 for_each、transform
排序算法 sort、stable_sort、partial_sort、
排序算法 包括對序列進行排序和合並的算法、搜索算法以及有序序列上的集合操做
數值算法 對容器內容進行數值計算dom
查找算法(13個):判斷容器中是否包含某個值函數
函數名spa |
頭文件指針 |
函數功能orm |
adjacent_find對象 |
<algorithm>排序 |
在iterator對標識元素範圍內,查找一對相鄰重複元素,找到則返回指向這對元素的第一個元素的ForwardIterator .不然返回last.重載版本使用輸入的二元操做符代替相等的判斷element |
函數原形rem |
template<class FwdIt> FwdIt adjacent_find(FwdIt first, FwdIt last); |
|
template<class FwdIt, class Pred> FwdIt adjacent_find(FwdIt first, FwdIt last, Pred pr); |
||
binary_search |
<algorithm> |
在有序序列中查找value,找到返回true.重載的版本實用指定的比較函數對象或函數指針來判斷相等 |
函數原形 |
template<class FwdIt, class T> bool binary_search(FwdIt first, FwdIt last, const T& val); |
|
template<class FwdIt, class T, class Pred> bool binary_search(FwdIt first, FwdIt last, const T& val,Pred pr); |
||
count |
<algorithm> |
利用等於操做符,把標誌範圍內的元素與輸入值比較,返回相等元素個數 |
函數原形 |
template<class InIt, class Dist> size_t count(InIt first, InIt last,const T& val, Dist& n); |
|
count_if |
<algorithm> |
利用輸入的操做符,對標誌範圍內的元素進行操做,返回結果爲true的個數 |
函數原形 |
template<class InIt, class Pred, class Dist> size_t count_if(InIt first, InIt last, Pred pr); |
|
equal_range |
<algorithm> |
功能相似equal,返回一對iterator,第一個表示lower_bound,第二個表示upper_bound |
函數原形 |
template<class FwdIt, class T> pair<FwdIt, FwdIt> equal_range(FwdIt first, FwdIt last,const T& val); |
|
template<class FwdIt, class T, class Pred> pair<FwdIt, FwdIt> equal_range(FwdIt first, FwdIt last,const T& val, Pred pr); |
||
find |
<algorithm> |
利用底層元素的等於操做符,對指定範圍內的元素與輸入值進行比較.當匹配時,結束搜索,返回該元素的一個InputIterator |
函數原形 |
template<class InIt, class T> InIt find(InIt first, InIt last, const T& val); |
|
find_end |
<algorithm> |
在指定範圍內查找"由輸入的另一對iterator標誌的第二個序列"的最後一次出現.找到則返回最後一對的第一個ForwardIterator,不然返回輸入的"另一對"的第一個ForwardIterator.重載版本使用用戶輸入的操做符代替等於操做 |
函數原形 |
template<class FwdIt1, class FwdIt2> FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2); |
|
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 find_end(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2, Pred pr); |
||
find_first_of |
<algorithm> |
在指定範圍內查找"由輸入的另一對iterator標誌的第二個序列"中任意一個元素的第一次出現。重載版本中使用了用戶自定義操做符 |
函數原形 |
template<class FwdIt1, class FwdIt2> FwdIt1 find_first_of(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2); |
|
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 find_first_of(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2, Pred pr); |
||
find_if |
<algorithm> |
使用輸入的函數代替等於操做符執行find |
|
template<class InIt, class Pred> InIt find_if(InIt first, InIt last, Pred pr); |
|
lower_bound |
<algorithm> |
返回一個ForwardIterator,指向在有序序列範圍內的能夠插入指定值而不破壞容器順序的第一個位置.重載函數使用自定義比較操做 |
函數原形 |
template<class FwdIt, class T> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val); |
|
template<class FwdIt, class T, class Pred> FwdIt lower_bound(FwdIt first, FwdIt last, const T& val, Pred pr); |
||
upper_bound |
<algorithm> |
返回一個ForwardIterator,指向在有序序列範圍內插入value而不破壞容器順序的最後一個位置,該位置標誌一個大於value的值.重載函數使用自定義比較操做 |
函數原形 |
template<class FwdIt, class T> FwdIt upper_bound(FwdIt first, FwdIt last, const T& val); |
|
template<class FwdIt, class T, class Pred> FwdIt upper_bound(FwdIt first, FwdIt last, const T& val, Pred pr); |
||
search |
<algorithm> |
給出兩個範圍,返回一個ForwardIterator,查找成功指向第一個範圍內第一次出現子序列(第二個範圍)的位置,查找失敗指向last1,重載版本使用自定義的比較操做 |
函數原形 |
template<class FwdIt1, class FwdIt2> FwdIt1 search(FwdIt1 first1, FwdIt1 last1,FwdIt2 first2, FwdIt2 last2); |
|
template<class FwdIt1, class FwdIt2, class Pred> FwdIt1 search(FwdIt1 first1, FwdIt1 last1, FwdIt2 first2, FwdIt2 last2, Pred pr); |
||
search_n |
<algorithm> |
在指定範圍內查找val出現n次的子序列。重載版本使用自定義的比較操做 |
函數原形 |
template<class FwdIt, class Dist, class T> FwdIt search_n(FwdIt first, FwdIt last,Dist n, const T& val); |
|
template<class FwdIt, class Dist, class T, class Pred> FwdIt search_n(FwdIt first, FwdIt last,Dist n, const T& val, Pred pr); |
堆算法(4個)
函數名 |
頭文件 |
函數功能 |
make_heap |
<algorithm> |
把指定範圍內的元素生成一個堆。重載版本使用自定義比較操做 |
函數原形 |
template<class RanIt> void make_heap(RanIt first, RanIt last); |
|
template<class RanIt, class Pred> void make_heap(RanIt first, RanIt last, Pred pr); |
||
pop_heap |
<algorithm> |
並不真正把最大元素從堆中彈出,而是從新排序堆。它把first和last-1交換,而後從新生成一個堆。可以使用容器的back來訪問被"彈出"的元素或者使用pop_back進行真正的刪除。重載版本使用自定義的比較操做 |
函數原形 |
template<class RanIt> void pop_heap(RanIt first, RanIt last); |
|
template<class RanIt, class Pred> void pop_heap(RanIt first, RanIt last, Pred pr); |
||
push_heap |
<algorithm> |
假設first到last-1是一個有效堆,要被加入到堆的元素存放在位置last-1,從新生成堆。在指向該函數前,必須先把元素插入容器後。重載版本使用指定的比較操做 |
函數原形 |
template<class RanIt>void push_heap(RanIt first, RanIt last); |
|
template<class RanIt, class Pred> void push_heap(RanIt first, RanIt last, Pred pr); |
||
sort_heap |
<algorithm> |
對指定範圍內的序列從新排序,它假設該序列是個有序堆。重載版本使用自定義比較操做 |
函數原形 |
template<class RanIt> void sort_heap(RanIt first, RanIt last); |
|
template<class RanIt, class Pred> void sort_heap(RanIt first, RanIt last, Pred pr); |
關係算法(8個)
函數名 |
頭文件 |
函數功能 |
equal |
<algorithm> |
若是兩個序列在標誌範圍內元素都相等,返回true。重載版本使用輸入的操做符代替默認的等於操做符 |
函數原形 |
template<class InIt1, class InIt2> bool equal(InIt1 first, InIt1 last, InIt2 x); |
|
template<class InIt1, class InIt2, class Pred> bool equal(InIt1 first, InIt1 last, InIt2 x, Pred pr); |
||
includes |
<algorithm> |
判斷第一個指定範圍內的全部元素是否都被第二個範圍包含,使用底層元素的<操做符,成功返回true。重載版本使用用戶輸入的函數 |
函數原形 |
template<class InIt1, class InIt2> bool includes(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2); |
|
template<class InIt1, class InIt2, class Pred> bool includes(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, Pred pr); |
||
lexicographical_compare |
<algorithm> |
比較兩個序列。重載版本使用用戶自定義比較操做 |
函數原形 |
template<class InIt1, class InIt2> bool lexicographical_compare(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2); |
|
template<class InIt1, class InIt2, class Pred> bool lexicographical_compare(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, Pred pr); |
||
max |
<algorithm> |
返回兩個元素中較大一個。重載版本使用自定義比較操做 |
函數原形 |
template<class T> const T& max(const T& x, const T& y); |
|
template<class T, class Pred> const T& max(const T& x, const T& y, Pred pr); |
||
max_element |
<algorithm> |
返回一個ForwardIterator,指出序列中最大的元素。重載版本使用自定義比較操做 |
函數原形 |
template<class FwdIt> FwdIt max_element(FwdIt first, FwdIt last); |
|
template<class FwdIt, class Pred> FwdIt max_element(FwdIt first, FwdIt last, Pred pr); |
||
min |
<algorithm> |
返回兩個元素中較小一個。重載版本使用自定義比較操做 |
函數原形 |
template<class T> const T& min(const T& x, const T& y); |
|
template<class T, class Pred> const T& min(const T& x, const T& y, Pred pr); |
||
min_element |
<algorithm> |
返回一個ForwardIterator,指出序列中最小的元素。重載版本使用自定義比較操做 |
函數原形 |
template<class FwdIt> FwdIt min_element(FwdIt first, FwdIt last); |
|
template<class FwdIt, class Pred> FwdIt min_element(FwdIt first, FwdIt last, Pred pr); |
||
mismatch |
<algorithm> |
並行比較兩個序列,指出第一個不匹配的位置,返回一對iterator,標誌第一個不匹配元素位置。若是都匹配,返回每一個容器的last。重載版本使用自定義的比較操做 |
函數原形 |
template<class InIt1, class InIt2> pair<InIt1, InIt2> mismatch(InIt1 first, InIt1 last, InIt2 x); |
|
template<class InIt1, class InIt2, class Pred> pair<InIt1, InIt2> mismatch(InIt1 first, InIt1 last, InIt2 x, Pred pr); |
集合算法(4個)
函數名 |
頭文件 |
函數功能 |
set_union |
<algorithm> |
構造一個有序序列,包含兩個序列中全部的不重複元素。重載版本使用自定義的比較操做 |
函數原形 |
template<class InIt1, class InIt2, class OutIt> OutIt set_union(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x); |
|
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_union(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2,OutIt x, Pred pr); |
||
set_intersection |
<algorithm> |
構造一個有序序列,其中元素在兩個序列中都存在。重載版本使用自定義的比較操做 |
函數原形 |
template<class InIt1, class InIt2, class OutIt> OutIt set_intersection(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x); |
|
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_intersection(InIt1 first1, InIt1 last1,InIt2 first2,InIt2 last2, OutIt x, Pred pr); |
||
set_difference |
<algorithm> |
構造一個有序序列,該序列僅保留第一個序列中存在的而第二個中不存在的元素。重載版本使用自定義的比較操做 |
函數原形 |
template<class InIt1, class InIt2, class OutIt> OutIt set_difference(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x); |
|
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred pr); |
||
set_symmetric_difference |
<algorithm> |
構造一個有序序列,該序列取兩個序列的對稱差集(並集-交集) |
函數原形 |
template<class InIt1, class InIt2, class OutIt> OutIt set_symmetric_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x); |
|
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt set_symmetric_difference(InIt1 first1, InIt1 last1, InIt2 first2, InIt2 last2, OutIt x, Pred pr); |
列組合算法(2個)
提供計算給定集合按必定順序的全部可能排列組合
函數名 |
頭文件 |
函數功能 |
next_permutation |
<algorithm> |
取出當前範圍內的排列,並從新排序爲下一個排列。重載版本使用自定義的比較操做 |
函數原形 |
template<class BidIt> bool next_permutation(BidIt first, BidIt last); |
|
template<class BidIt, class Pred> bool next_permutation(BidIt first, BidIt last, Pred pr); |
||
prev_permutation |
<algorithm> |
取出指定範圍內的序列並將它從新排序爲上一個序列。若是不存在上一個序列則返回false。重載版本使用自定義的比較操做 |
函數原形 |
template<class BidIt> bool prev_permutation(BidIt first, BidIt last); |
|
template<class BidIt, class Pred> bool prev_permutation(BidIt first, BidIt last, Pred pr); |
排序和通用算法(14個):提供元素排序策略
函數名 |
頭文件 |
函數功能 |
inplace_merge |
<algorithm> |
合併兩個有序序列,結果序列覆蓋兩端範圍。重載版本使用輸入的操做進行排序 |
函數原形 |
template<class BidIt> void inplace_merge(BidIt first, BidIt middle, BidIt last); |
|
template<class BidIt, class Pred> void inplace_merge(BidIt first, BidIt middle, BidIt last, Pred pr); |
||
merge |
<algorithm> |
合併兩個有序序列,存放到另外一個序列。重載版本使用自定義的比較 |
函數原形 |
template<class InIt1, class InIt2, class OutIt> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x); |
|
template<class InIt1, class InIt2, class OutIt, class Pred> OutIt merge(InIt1 first1, InIt1 last1,InIt2 first2, InIt2 last2, OutIt x, Pred pr); |
||
nth_element |
<algorithm> |
將範圍內的序列從新排序,使全部小於第n個元素的元素都出如今它前面,而大於它的都出如今後面。重載版本使用自定義的比較操做 |
函數原形 |
template<class RanIt> void nth_element(RanIt first, RanIt nth, RanIt last); |
|
template<class RanIt, class Pred> void nth_element(RanIt first, RanIt nth, RanIt last, Pred pr); |
||
partial_sort |
<algorithm> |
對序列作部分排序,被排序元素個數正好能夠被放到範圍內。重載版本使用自定義的比較操做 |
函數原形 |
template<class RanIt> void partial_sort(RanIt first, RanIt middle, RanIt last); |
|
template<class RanIt, class Pred> void partial_sort(RanIt first, RanIt middle, RanIt last, Pred pr); |
||
partial_sort_copy |
<algorithm> |
與partial_sort相似,不過將通過排序的序列複製到另外一個容器 |
函數原形 |
template<class InIt, class RanIt> RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2); |
|
template<class InIt, class RanIt, class Pred> RanIt partial_sort_copy(InIt first1, InIt last1,RanIt first2, RanIt last2, Pred pr); |
||
partition |
<algorithm> |
對指定範圍內元素從新排序,使用輸入的函數,把結果爲true的元素放在結果爲false的元素以前 |
函數原形 |
template<class BidIt, class Pred> BidIt partition(BidIt first, BidIt last, Pred pr); |
|
random_shuffle |
<algorithm> |
對指定範圍內的元素隨機調整次序。重載版本輸入一個隨機數產生操做 |
函數原形 |
template<class RanIt> void random_shuffle(RanIt first, RanIt last); |
|
template<class RanIt, class Fun> void random_shuffle(RanIt first, RanIt last, Fun& f); |
||
reverse |
<algorithm> |
將指定範圍內元素從新反序排序 |
函數原形 |
template<class BidIt> void reverse(BidIt first, BidIt last); |
|
reverse_copy |
<algorithm> |
與reverse相似,不過將結果寫入另外一個容器 |
函數原形 |
template<class BidIt, class OutIt> OutIt reverse_copy(BidIt first, BidIt last, OutIt x); |
|
rotate |
<algorithm> |
將指定範圍內元素移到容器末尾,由middle指向的元素成爲容器第一個元素 |
函數原形 |
template<class FwdIt> void rotate(FwdIt first, FwdIt middle, FwdIt last); |
|
rotate_copy |
<algorithm> |
與rotate相似,不過將結果寫入另外一個容器 |
函數原形 |
template<class FwdIt, class OutIt> OutIt rotate_copy(FwdIt first, FwdIt middle, FwdIt last, OutIt x); |
|
sort |
<algorithm> |
以升序從新排列指定範圍內的元素。重載版本使用自定義的比較操做 |
函數原形 |
template<class RanIt> void sort(RanIt first, RanIt last); |
|
template<class RanIt, class Pred> void sort(RanIt first, RanIt last, Pred pr); |
||
stable_sort |
<algorithm> |
與sort相似,不過保留相等元素之間的順序關係 |
函數原形 |
template<class BidIt> void stable_sort(BidIt first, BidIt last); |
|
template<class BidIt, class Pred> void stable_sort(BidIt first, BidIt last, Pred pr); |
||
stable_partition |
<algorithm> |
與partition相似,不過不保證保留容器中的相對順序 |
函數原形 |
template<class FwdIt, class Pred> FwdIt stable_partition(FwdIt first, FwdIt last, Pred pr); |
刪除和替換算法(15個)
函數名 |
頭文件 |
函數功能 |
copy |
<algorithm> |
複製序列 |
函數原形 |
template<class InIt, class OutIt> OutIt copy(InIt first, InIt last, OutIt x); |
|
copy_backward |
<algorithm> |
與copy相同,不過元素是以相反順序被拷貝 |
函數原形 |
template<class BidIt1, class BidIt2> BidIt2 copy_backward(BidIt1 first, BidIt1 last, BidIt2 x); |
|
iter_swap |
<algorithm> |
交換兩個ForwardIterator的值 |
函數原形 |
template<class FwdIt1, class FwdIt2> void iter_swap(FwdIt1 x, FwdIt2 y); |
|
remove |
<algorithm> |
刪除指定範圍內全部等於指定元素的元素。注意,該函數不是真正刪除函數。內置函數不適合使用remove和remove_if函數 |
函數原形 |
template<class FwdIt, class T> FwdIt remove(FwdIt first, FwdIt last, const T& val); |
|
remove_copy |
<algorithm> |
將全部不匹配元素複製到一個制定容器,返回OutputIterator指向被拷貝的末元素的下一個位置 |
函數原形 |
template<class InIt, class OutIt, class T> OutIt remove_copy(InIt first, InIt last, OutIt x, const T& val); |
|
remove_if |
<algorithm> |
刪除指定範圍內輸入操做結果爲true的全部元素 |
函數原形 |
template<class FwdIt, class Pred> FwdIt remove_if(FwdIt first, FwdIt last, Pred pr); |
|
remove_copy_if |
<algorithm> |
將全部不匹配元素拷貝到一個指定容器 |
函數原形 |
template<class InIt, class OutIt, class Pred> OutIt remove_copy_if(InIt first, InIt last, OutIt x, Pred pr); |
|
replace |
<algorithm> |
將指定範圍內全部等於vold的元素都用vnew代替 |
函數原形 |
template<class FwdIt, class T> void replace(FwdIt first, FwdIt last,const T& vold, const T& vnew); |
|
replace_copy |
<algorithm> |
與replace相似,不過將結果寫入另外一個容器 |
函數原形 |
template<class InIt, class OutIt, class T> OutIt replace_copy(InIt first, InIt last, OutIt x,const T& vold, const T& vnew); |
|
replace_if |
<algorithm> |
將指定範圍內全部操做結果爲true的元素用新值代替 |
函數原形 |
template<class FwdIt, class Pred, class T> void replace_if(FwdIt first, FwdIt last,Pred pr, const T& val); |
|
replace_copy_if |
<algorithm> |
與replace_if,不過將結果寫入另外一個容器 |
函數原形 |
template<class InIt, class OutIt, class Pred, class T> OutIt replace_copy_if(InIt first, InIt last, OutIt x, Pred pr, const T& val); |
|
swap |
<algorithm> |
交換存儲在兩個對象中的值 |
函數原形 |
template<class T> void swap(T& x, T& y); |
|
swap_range |
<algorithm> |
將指定範圍內的元素與另外一個序列元素值進行交換 |
函數原形 |
template<class FwdIt1, class FwdIt2> FwdIt2 swap_ranges(FwdIt1 first, FwdIt1 last, FwdIt2 x); |
|
unique |
<algorithm> |
清除序列中重複元素,和remove相似,它也不能真正刪除元素。重載版本使用自定義比較操做 |
函數原形 |
template<class FwdIt> FwdIt unique(FwdIt first, FwdIt last); |
|
template<class FwdIt, class Pred> FwdIt unique(FwdIt first, FwdIt last, Pred pr); |
||
unique_copy |
<algorithm> |
與unique相似,不過把結果輸出到另外一個容器 |
函數原形 |
template<class InIt, class OutIt> OutIt unique_copy(InIt first, InIt last, OutIt x); |
|
template<class InIt, class OutIt, class Pred> OutIt unique_copy(InIt first, InIt last, OutIt x, Pred pr); |
生成和變異算法(6個)
函數名 |
頭文件 |
函數功能 |
fill |
<algorithm> |
將輸入值賦給標誌範圍內的全部元素 |
函數原形 |
template<class FwdIt, class T> void fill(FwdIt first, FwdIt last, const T& x); |
|
fill_n |
<algorithm> |
將輸入值賦給first到first+n範圍內的全部元素 |
函數原形 |
template<class OutIt, class Size, class T> void fill_n(OutIt first, Size n, const T& x); |
|
for_each |
<algorithm> |
用指定函數依次對指定範圍內全部元素進行迭代訪問,返回所指定的函數類型。該函數不得修改序列中的元素 |
函數原形 |
template<class InIt, class Fun> Fun for_each(InIt first, InIt last, Fun f); |
|
generate |
<algorithm> |
連續調用輸入的函數來填充指定的範圍 |
函數原形 |
template<class FwdIt, class Gen> void generate(FwdIt first, FwdIt last, Gen g); |
|
generate_n |
<algorithm> |
與generate函數相似,填充從指定iterator開始的n個元素 |
函數原形 |
template<class OutIt, class Pred, class Gen> void generate_n(OutIt first, Dist n, Gen g); |
|
transform |
<algorithm> |
將輸入的操做做用與指定範圍內的每一個元素,併產生一個新的序列。重載版本將操做做用在一對元素上,另一個元素來自輸入的另一個序列。結果輸出到指定容器 |
函數原形 |
template<class InIt, class OutIt, class Unop> OutIt transform(InIt first, InIt last, OutIt x, Unop uop); |
|
template<class InIt1, class InIt2, class OutIt, class Binop> OutIt transform(InIt1 first1, InIt1 last1, InIt2 first2,OutIt x, Binop bop); |
算數算法(4個)
函數名 |
頭文件 |
函數功能 |
accumulate |
<numeric> |
iterator對標識的序列段元素之和,加到一個由val指定的初始值上。重載版本再也不作加法,而是傳進來的二元操做符被應用到元素上 |
函數原形 |
template<class InIt, class T> T accumulate(InIt first, InIt last, T val); |
|
template<class InIt, class T, class Pred> T accumulate(InIt first, InIt last, T val, Pred pr); |
||
partial_sum |
<numeric> |
建立一個新序列,其中每一個元素值表明指定範圍內該位置前全部元素之和。重載版本使用自定義操做代替加法 |
函數原形 |
template<class InIt, class OutIt> OutIt partial_sum(InIt first, InIt last,OutIt result); |
|
template<class InIt, class OutIt, class Pred> OutIt partial_sum(InIt first, InIt last,OutIt result, Pred pr); |
||
product |
<numeric> |
對兩個序列作內積(對應元素相乘,再求和)並將內積加到一個輸入的初始值上。重載版本使用用戶定義的操做 |
函數原形 |
template<class InIt1, class InIt2, class T> T product(InIt1 first1, InIt1 last1,Init2 first2, T val); |
|
template<class InIt1, class InIt2, class T,class Pred1, class Pred2> T product(InIt1 first1, InIt1 last1,Init2 first2, T val, Pred1 pr1, Pred2 pr2); |
||
adjacent_difference |
<numeric> |
建立一個新序列,新序列中每一個新值表明當前元素與上一個元素的差。重載版本用指定二元操做計算相鄰元素的差 |
函數原形 |
template<class InIt, class OutIt> OutIt adjacent_difference(InIt first, InIt last,OutIt result); |
|
template<class InIt, class OutIt, class Pred> OutIt adjacent_difference(InIt first, InIt last,OutIt result, Pred pr); |
經常使用的查找算法:
adjacent_find()( adjacent 是鄰近的意思),binary_search(),count(), count_if(),equal_range(),find(),find_if()。
經常使用的排序算法:
merge(),sort(),random_shuffle()(shuffle是洗牌的意思) ,reverse()。
經常使用的拷貝和替換算法:
copy(), replace(), replace_if(),swap()
經常使用的算術和生成算法:
accumulate()( accumulate 是求和的意思),fill(),。
經常使用的集合算法:
set_union(),set_intersection(), set_difference()。
經常使用的遍歷算法:
for_each(), transform()( transform 是變換的意思)