STL模板庫第十一日 : 算法基礎

前言:

 算法部分主要由頭文件<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、
    • 排序算法 包括對序列進行排序和合並的算法、搜索算法以及有序序列上的集合操作
    • 數值算法 對容器內容進行數值計算


函數名

頭文件

函數功能

adjacent_find

<algorithm>

在iterator對標識元素範圍內,查找一對相鄰重複元素,找到則返回指向這對元素的第一個元素的ForwardIterator .否則返回last.重載版本使用輸入的二元操作符代替相等的判斷

函數原形

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);

函數名

頭文件

函數功能

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);

函數名

頭文件

函數功能

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);

函數名

頭文件

函數功能

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);

函數名

頭文件

函數功能

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);

函數名

頭文件

函數功能

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);

函數名

頭文件

函數功能

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);

函數名

頭文件

函數功能

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);

函數名

頭文件

函數功能

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);


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章