STL 之replace,replace_if,replace_copy,replace_copy_if
生活随笔
收集整理的這篇文章主要介紹了
STL 之replace,replace_if,replace_copy,replace_copy_if
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
作用:用一個新值替換指定區(qū)間內所有的指定元素。
#include?<algorithm>?? template?<class?forwardItr,class?Type>?? void?replace(forwardItr?first,?forwardItr?last,const?Type&?oldValue?const?Type&?newValue);?? ?? template?<class?forwardItr,?class?unaryPredicate,class?Type>?? void?replace_if(forwardItr?first,?forwardItr?last,?unaryPredicate?op,const?Type&?newValue);?? ?? template?<class?inputItr,class?outputItr,class?Type>?? outputItr?replace_copy(inputItr?first1,?inputItr?last1,?outputItr?destFirst,const?Type&?oldValue,?const?Type&?newValue);?? ?? template?<class?inputItr,class?outputItr,?class?unaryPredicate>?? outputItr?replace_copy_if(inputItr?first1,?inputItr?last1,?outputItr?destFirst,?unaryPredicate?op,const?Type&?newValue);??
示例代碼: #include?<iostream>?? #include?<list>?? ?? #include?<string>?? #include?<numeric>?? #include?<iterator>?? #include?<vector>?? #include?<functional>?? ?? #include?<algorithm>?? ?? using?namespace?std;?? ?? bool?lessThanEqual50(int?num)?{?? ????return?(num?<=?50);?? }?? ?? int?main()?{?? ????char?cList[10]?=?{'A','a','A','B','A','c','D','e','F','A'};?? ????vector<char>?charList(cList,cList+10);?? ????ostream_iterator<char>?screen(cout,?"?");?? ?? ????cout?<<?"charList:"?<<?endl;?? ????copy(charList.begin(),charList.end(),screen);?? ????cout?<<?endl;?? ????//?replace?? ????//?將容器中的A替換為Z?? ????replace(charList.begin(),charList.end(),'A','Z');?? ????cout?<<?"charList.replace?A?->?Z:"?<<?endl;?? ????copy(charList.begin(),charList.end(),screen);?? ????cout?<<?endl;?? ????//?replace_if?? ????//?將所有的大寫字母替換為*?? ????replace_if(charList.begin(),charList.end(),isupper,'*');?? ????cout?<<?"charList.replace_if?Upper->*"?<<?endl;?? ????copy(charList.begin(),charList.end(),screen);?? ????cout?<<?endl;?? ?? ????int?listi[10]?=?{12,34,56,21,34,78,34,55,12,25};?? ????vector<int>?intList(listi,listi+10);?? ????ostream_iterator<int>?screenInt(cout,?"?");?? ????cout?<<?"intList:"?<<?endl;?? ????copy(intList.begin(),intList.end(),screenInt);?? ????cout?<<?endl;?? ?? ????vector<int>?temp1(10);?? ????//?將intList中34全部替換為0,并輸出到temp1中,不改變intList?? ????replace_copy(intList.begin(),intList.end(),temp1.begin(),34,0);?? ????cout?<<?"intList.replace_copy:"?<<?endl;?? ????copy(intList.begin(),intList.end(),screenInt);?? ????cout?<<?endl;?? ????cout?<<?"temp1:"?<<?endl;?? ????copy(temp1.begin(),temp1.end(),screenInt);?? ????cout?<<?endl;?? ?? ????vector<int>?temp2(10);?? ????//?將intList中小于50的全部替換為50,并輸出到temp2中,不改變intList?? ????replace_copy_if(intList.begin(),intList.end(),temp2.begin(),lessThanEqual50,50);?? ????cout?<<?"intList.replace_copy_if:"?<<?endl;?? ????copy(intList.begin(),intList.end(),screenInt);?? ????cout?<<?endl;?? ????cout?<<?"temp2:"?<<?endl;?? ????copy(temp2.begin(),temp2.end(),screenInt);?? ????cout?<<?endl;?? ?? ????return?0;?? }??
聲明:
示例代碼:
運行結果:
charList:
A a A B A c D e F A
charList.replace A -> Z:
Z a Z B Z c D e F Z
charList.replace_if Upper->*
* a * * * c * e * *
intList:
12 34 56 21 34 78 34 55 12 25
intList.replace_copy:
12 34 56 21 34 78 34 55 12 25
temp1:
12 0 56 21 0 78 0 55 12 25
intList.replace_copy_if:
12 34 56 21 34 78 34 55 12 25
temp2:
50 50 56 50 50 78 50 55 50 50
總結
以上是生活随笔為你收集整理的STL 之replace,replace_if,replace_copy,replace_copy_if的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STL 之reverse,reverse
- 下一篇: STL 之adjacent_find,