STL修改演算法__演算法

來源:互聯網
上載者:User

修改演算法:

演算法 用法 說明
copy OutIt copy(InIt first,InIt last,OutIt x); 將[first,last)之間的元素複製到x指定的位置
copy_backward BidIt2 copy_backward(BidIt1 first,BidIt1 last,BidIt2 x); 將[first,last)之間的元素複製到x指定位置的前面
fill void fill(FwdIt first,FwdIt last,const T& x); 將[first,last)之間的部分用元素x填充
fill_n void fill_n(OutIt first, Size n,const T& x); 從first開始向容器中填充n個元素x
generate void generate(FwdIt first,FwdIt last, Gen g); 調用函數g()填充[first,last)之間的元素
generate_n void generate_n(OutIt first,size n, Gen g); 調用函數g()填充從first開始的n個元素
remove FwdIt remove(FwdIt first,FwdIt last,const T& val); 從[first,last)中刪除元素val,返回新區間中最後一個元素後的位置
remove_if FwdIt remove_if(FwdIt first,FwdIt last, Pred pr); 從[first,last)中刪除所有能使函數pr返回true的元素,返回新區間最後一個元素後的位置
remove_copy OutIt remove_copy(InIt first,InIt last,OutIt x,const T& val); 從[first,last)中刪除元素val,並把其餘的元素複製到x開始的區間中,返回被拷貝的最後一個元素的位置
remover_copy_if OutIt remove_copy_if(InIt first,InIt last,OutIt x,Pred pr); 從[first,last)中刪除所有能使函數pr返回true的元素,並把其餘元素複製到x開始的空間,返回被拷貝最後一個元素的位置
replace void replace(FwdIt first,FwdIt last,const T& vold,const T& vnew); 用vnew替換[first,last)中所有值為vold的元素
replace_if void replace_if(FwdIt first,FwdIt last,Pred pr,const T& val); 用val替換[first,last)中所有能使函數pr返回true的元素
replace_copy OutIt replace_copy(InIt first,InIt last,OutIt x,const T& vold,const T& vnew); 把[first,last)中的所有元素複製到以x開始的空間中,如果拷貝的元素中存在值為vold的元素,則以vnew替換,返回拷貝到目標容器的最後一個元素的後一個位置
replace_copy_if OutIt replace_copy_if(InIt first,InIt last,OutIt x,Pred pr,const T& val); 把[first,last)中的所有元素複製到以x開始的空間中,如果拷貝的元素中存在能夠使函數pr返回true的元素,則以vnew替換,返回拷貝到目標容器的最後一個元素的後一個位置
swap void swap(Container &c1,Container &c2); 交換容器c1與c2的元素
iter_swap void iter_swap(FwdIt1 x,FwdIt2 y); 交換迭代器x,y所指元素的值
swap_ranges FwdIt2 swap_ranges(FwdIt1 first,FwdIt1 last,FwdIt2 x); 用起始於x的連續元素交換區間[first,last)的所有元素,返回第二個區間的最後一個元素的下一個位置
sort void sort(RanIt first, RanIt last);
void sort(RanIt first, RanIt last, Pred pr);
將區間[first,last)中的元素按升序排序,第二種形式pr用於設定比較函數
merge OutIt merge(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x);
OutIt merge(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x, Pred pr);
合并兩個有序區間[first1,last1)和[first2,last2),並將合并後的元素儲存到x開始的容器裡,第二種形式pr(elem1,elem2)指定比較規則
inplace_merge void inplace_merge(BidIt first, BidIt middle, BidIt last);
void inplace_merge(BidIt first, BidIt middle, BidIt last, Pred pr);
合并區間[first,middle)和[middle,last),並將合并後的元素儲存到first開始的區間,第二種形式pr(elem1,elem2)指定比較規則
reverse void reverse(BidIt first, BidIt last); 將區間[first,last)的元素順序倒置
reverse_copy OutIt reverse_copy(BidIt first, BidIt last,OutIt x); 將區間[first,last)的元素拷貝到x開始的區間,導致拷貝的元素順序,源區間沒有被修改,返回拷貝到目標區間的最後一個元素的下一個位置
rotate void rotate(FwdIt first,FwdIt middle,FwdIt last); 交換區間[first,middle)和[middle,last)的元素位置
rotate_copy OutIt rotate_copy(FwdIt first,FwdIt middle,FwdIt last,OutIt x); 將區間[first,last)的元素拷貝到x開始的容器中,拷貝時交換[first,middle)和[middle,last)的位置,源區間不發生修改,返回拷貝到目標區間的最後一個元素的下一位
random_shuffle void random_shuffle(RanIt first, RanIt last);
void random_shuffle(RanIt first, RanIt last, Fun& f);
對區間[first,last)上的元素進行隨機排序,f用於指定排序函數
transform OutIt transform(InIt first,InIt last,OutIt x, Unop uop); 對區間[first,last)上的所有元素執行uop(elem)操作,結果放到以x開始的區間內,返回拷貝到目標端的最後一個元素的下一位
transform OutIt transform(InIt1 first1,InIt1 last1,InIt2 first2,OutIt x, Binop bop); 對區間[first1,last1)和以first2開始的區間的元素執行二元運算bop(elem1,elem2),結果放到以x開始的區間內,返回拷貝到目標端的最後一個元素的下一位
set_itersection OutIt set_intersection(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x);
OutIt set_intersection(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x, Pred pr);
求兩個區間[first1,last1),[first2,last2)的交集,儲存到x開始的容器裡,源區間元素沒有被修改,pr用於指定操作函數,返回拷貝到目標區間的最後一個元素的下一個位置
set_union OutIt set_union(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x);
OutIt set_union(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x, Pred pr);
求兩個區間[first1,last1),[first2,last2)的並集,儲存到x開始的容器裡,源區間元素沒有修改,pr用於指定操作函數,返回拷貝到目標區間的最後一個元素的下一位
set_difference OutIt set_difference(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x);
OutIt set_difference(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x, Pred pr);
求兩個集合[first1,last1),[first2,last2)的差集,儲存到x開始的容器裡,源區間元素沒有被修改,pr用於指定操作函數,返回拷貝到目標區間的最後一個元素的下一個位置
set_symmetric_

difference

OutIt set_symmetric_difference(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x);
OutIt set_symmetric_difference(InIt1 first1,InIt1 last1,InIt2 first2,InIt2 last2,OutIt x, Pred pr);
求集合[first1,last1),[first2,last2)的對稱差(即(A-B)+(B-A)),儲存到x開始的容器裡,源區間元素沒有被修改,pr用於指定操作函數,返回拷貝到目標區間最後一個元素的下一個位置

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.