STL算法replace,replace_if,replace_copy.replace_copy_if

来源:互联网 发布:大数据时代的管理变革 编辑:程序博客网 时间:2024/05/17 06:54

所有容器适用
replace(b,e,ov,nv)      //把oldvalue替换成newvalue
replace_if(b,e,p,v)     //把符合p条件的替换成v
replace_copy(b1,e1,b2,ov,nv)
replace_copy_if(b1,e1,b2,p,v)

[cpp] view plain copy
  1. /**------http://blog.csdn.net/u010579068------**/  
  2. #include<iostream>  
  3. #include<cstdio>  
  4. #include<string>  
  5. #include<vector>  
  6. #include<list>  
  7. #include<deque>  
  8. #include<algorithm>  
  9. using namespace std;  
  10.   
  11. /***************************************** 
  12. //所有容器适用 
  13. replace(b,e,ov,nv)      //把oldvalue替换成newvalue 
  14. replace_if(b,e,p,v)     //把符合p条件的替换成v 
  15. replace_copy(b1,e1,b2,ov,nv) 
  16. replace_copy_if(b1,e1,b2,p,v) 
  17. *****************************************/  
  18. /**---------------------------------------------------------------------------------- 
  19.  
  20. ----------------------------------------------------------------------------------**/  
  21. /************************************************************************************* 
  22. std::replace                     所有排序容器适用                           algorithm 
  23. -------------------------------------------------------------------------------------- 
  24. template < class ForwardIterator, class T > 
  25.   void replace ( ForwardIterator first, ForwardIterator last, 
  26.                  const T& old_value, const T& new_value ); 
  27.  
  28. //eg: 
  29. template < class ForwardIterator, class T > 
  30.   void replace ( ForwardIterator first, ForwardIterator last, 
  31.                  const T& old_value, const T& new_value ) 
  32. { 
  33.   for (; first != last; ++first) 
  34.     if (*first == old_value) *first=new_value; 
  35. } 
  36. *************************************************************************************/  
  37.   
  38. /************************************************************************************* 
  39. std::replace_if                   所有排序容器适用                          algorithm 
  40. -------------------------------------------------------------------------------------- 
  41. template < class ForwardIterator, class Predicate, class T > 
  42.   void replace_if ( ForwardIterator first, ForwardIterator last, 
  43.                     Predicate pred, const T& new_value ); 
  44.  
  45. //eg: 
  46. template < class ForwardIterator, class Predicate, class T > 
  47.   void replace_if ( ForwardIterator first, ForwardIterator last, 
  48.                     Predicate pred, const T& new_value ) 
  49. { 
  50.   for (; first != last; ++first) 
  51.     if (pred(*first)) *first=new_value; 
  52. } 
  53. *************************************************************************************/  
  54.   
  55. /************************************************************************************* 
  56. std::replace_copy                  所有排序容器适用                         algorithm 
  57. -------------------------------------------------------------------------------------- 
  58. template < class InputIterator, class OutputIterator, class T > 
  59.   OutputIterator replace_copy ( InputIterator first, InputIterator last, 
  60.                                 OutputIterator result, 
  61.                                 const T& old_value, const T& new_value ); 
  62.  
  63. //eg: 
  64. template < class InputIterator, class OutputIterator, class T > 
  65.   OutputIterator replace_copy ( InputIterator first, InputIterator last, 
  66.                                 OutputIterator result, const T& old_value, const T& new_value ) 
  67. { 
  68.   for (; first != last; ++first, ++result) 
  69.     *result = (*first==old_value)? new_value: *first; 
  70.   return result; 
  71. } 
  72. *************************************************************************************/  
  73.   
  74. /************************************************************************************* 
  75. std::replace_copy_if                  所有排序容器适用                     algorithm 
  76. -------------------------------------------------------------------------------------- 
  77. template < class InputIterator, class OutputIterator, class Predicate, class T > 
  78.   OutputIterator replace_copy_if ( InputIterator first, InputIterator last, 
  79.                                    OutputIterator result, Predicate pred, 
  80.                                    const T& new_value ); 
  81.  
  82. //eg: 
  83. template < class InputIterator, class OutputIterator, class Predicate, class T > 
  84.   OutputIterator replace_copy_if ( InputIterator first, InputIterator last, 
  85.                                    OutputIterator result, Predicate pred, 
  86.                                    const T& new_value ) 
  87. { 
  88.   for (; first != last; ++first, ++result) 
  89.     *result = (pred(*first))? new_value: *first; 
  90.   return result; 
  91. } 
  92. *************************************************************************************/  
  93.   
  94. int main()  
  95. {  
  96.     int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };  
  97.     vector<int> myvector (myints, myints+8);            // 10 20 30 30 20 10 10 20  
  98. //replace(b,e,ov,nv)  
  99.     replace (myvector.begin(), myvector.end(), 20, 99); // 10 99 30 30 99 10 10 99  
  100.   
  101.     cout << "myvector contains:";  
  102.     for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)  
  103.         cout << " " << *it;  
  104.   
  105.     cout << endl;  
  106.     /**--------------------------------------------------------------------------------**/  
  107.   
  108.     myvector.clear();  
  109.     vector<int>::iterator it;  
  110.   
  111.     // set some values:  
  112.     for (int i=1; i<10; i++) myvector.push_back(i);          // 1 2 3 4 5 6 7 8 9  
  113.   
  114.     replace_if (myvector.begin(), myvector.end(), bind2nd(modulus<int>(),2), 0); // 0 2 0 4 0 6 0 8 0  
  115.   
  116.     cout << "myvector contains:";  
  117.     for (it=myvector.begin(); it!=myvector.end(); ++it)  
  118.         cout << " " << *it;  
  119.   
  120.     cout << endl;  
  121.     /**--------------------------------------------------------------------------------**/  
  122.   
  123. //    int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };  
  124.   
  125.     list<int> mylist (8);  
  126.     replace_copy (myints, myints+8, mylist.begin(), 20, 66);  
  127.   
  128.     cout << "mylist  contains: ";  
  129.     for (list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)  
  130.         cout << " " << *it;  
  131.   
  132.     cout << endl;  
  133.     /**--------------------------------------------------------------------------------**/  
  134.   
  135.     deque<int> first,second;  
  136.     deque<int>::iterator id;  
  137.   
  138.     // set some values:  
  139.     for (int i=1; i<10; i++) first.push_back(i);          // 1 2 3 4 5 6 7 8 9  
  140.   
  141.     second.resize(first.size());   // allocate space  
  142.     replace_copy_if (first.begin(), first.end(), second.begin(), not1(bind2nd(modulus<int>(),2)), 0);  
  143.     // 1 0 3 0 5 0 7 0 9  
  144.   
  145.     cout << "second  contains: ";  
  146.     for (id=second.begin(); id!=second.end(); ++id)  
  147.         cout << " " << *id;  
  148.   
  149.     cout << endl;  
  150.   
  151.   
  152.     return 0;  
  153. }  

0 0
原创粉丝点击