C + + Primer learning ...
Simple recording of My Learning process (code-based)
All containers apply
Replace (B,E,OV,NV)//change oldvalue to NewValue
Replace_if (B,E,P,V)//replace the P condition with V
Replace_copy (B1,E1,B2,OV,NV)
Replace_copy_if (B1,E1,B2,P,V)
/**------http://blog.csdn.net/u010579068------**/#include <iostream> #include <cstdio> #include < string> #include <vector> #include <list> #include <deque> #include <algorithm>using namespace std;/*****************************************//all containers for replace (B,E,OV,NV)//oldvalue replacement to Newvaluereplace _if (B,E,P,V)//replace the P-condition with vreplace_copy (B1,E1,B2,OV,NV) replace_copy_if (b1,e1,b2,p,v) **************************** *************//**---------------------------------------------------------------------------------------------- ----------------------------------------------------------------------**//************************************* Std::replace all sorting containers for Algori tHM--------------------------------------------------------------------------------------Template < class ForwardIterator, class T > void replace (forwarditerator first, ForwardIterator last, Const t& Old_value, const t& new_value),//eg:template < class ForwardIterator, class T > void replace ( ForwardIterator first, ForwardIterator last, const t& old_value, const t& new_value) {for (; fi rst! = Last; ++first) if (*first = = Old_value) *first=new_value;} *************************************************************************************//************************ std::replace_if All sorting containers apply Algorithm--------------------------------------------------------------------------------------template < Class ForwardIterator, class predicate, class T > void Replace_if (forwarditerator first, ForwardIterator last, predicate pred, const t& new_value)//eg:template < class ForwardIterator, class predicate, class T > void replace_if (forwarditerator first, ForwardIterator last, PredicaTe pred, const t& new_value) {for (; first! = last; ++first) if (pred (*first)) *first=new_value;} *************************************************************************************//************************ Std::replace_copy All sorting containers apply Algorithm--------------------------------------------------------------------------------------template < Class Inputiterator, Class Outputiterator, class T > Outputiterator replace_copy (inputiterator first, Inputiterator Last, outputiterator result, const t& old_value, const T & New_value)//eg:template < class Inputiterator, class Outputiterator, class T > Outputiterator replace_copy (Inputiterator first, inputiterator last, outputiterator result, const t& Old_value, C Onst t& New_value) {for (; first! = last; ++first,++result) *result = (*first==old_value)? New_value: *first; return result;} *************************************************************************************//************************ std::replace_copy_if All sorting containers apply Algorithm--------------------------------------------------------------------------------------template < Class Inputiterator, class Outputiterator, class predicate, class T > Outputiterator replace_copy_if (inputiterator F Irst, Inputiterator last, outputiterator result, predicate pred, Const t& new_value);//eg:template < class Inputiterator, class Outputiterator, class predicate, class T > Outputiterator replace_copy_if (inputiterator First, Inputiterator last, Outputi Terator result, predicate pred, const t& NEW_VALue) {for (; first! = last; ++first, ++result) *result = (pred (*first))? New_value: *first; return result;} /int Main () {int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20}; Vector<int> Myvector (myints, myints+8); Ten 20//replace (B,E,OV,NV) replace (Myvector.begin (), Myvector.end (), 20, 99); Ten to ten cout << "myvector contains:"; For (Vector<int>::iterator It=myvector.begin (); It!=myvector.end (); ++it) cout << "" << *it; cout << Endl; /**--------------------------------------------------------------------------------**/myvector.clear (); Vector<int>::iterator it; Set some values:for (int i=1; i<10; i++) Myvector.push_back (i); 1 2 3 4 5 6 7 8 9 replace_if (Myvector.begin (), Myvector.end (), bind2nd (Modulus<int> (), 2), 0); 0 2 0 4 0 6 0 8 0 cout << "myvector contains:"; For (It=myvector.begin (); It!=myvector.end (); ++it) cout << "" << *it; cout << Endl; /**--------------------------------------------------------------------------------**///int myints[] = {10, 20, 30, 30, 20, 10, 10, 20}; List<int> MyList (8); Replace_copy (myints, Myints+8, Mylist.begin (), 20, 66); cout << "mylist contains:"; For (List<int>::iterator It=mylist.begin (); It!=mylist.end (); ++it) cout << "" << *it; cout << Endl; /**--------------------------------------------------------------------------------**/deque<int> First, Second Deque<int>::iterator ID; Set some values:for (int i=1; i<10; i++) First.push_back (i); 123456789second.resize(first.size()); Allocate Space replace_copy_if (First.begin (), First.end (), Second.begin (), Not1 (bind2nd (Modulus<int> (), 2)), 0); 1 0 3 0 5 0 7 0 9 cout << "second contains:"; For (Id=second.begin (); Id!=second.end (); ++id) cout << "" << *id; cout << Endl; return 0;}
Stl_ algorithm _ Replace (replace, replace_copy, replace_if, replace_copy_if)