Transform

  1. /*//////////////////////////////// 
  2. template < class InputIterator, class OutputIterator, class UnaryOperator > 
  3.   OutputIterator transform ( InputIterator first1,  // 源容器的起始地址 
  4.                             InputIterator last1,    // 源容器的终止地址 
  5.                             OutputIterator result,  // 目标容器的起始地址 
  6.                             UnaryOperator op );     // 函数指针 
  7. // typedef 目标容器元素类型 (*UnaryOperator)(源容器元素类型);

 

  1. template < class InputIterator1, class InputIterator2, 
  2.            class OutputIterator, class BinaryOperator > 
  3.   OutputIterator transform ( InputIterator1 first1,     // 源容器1的起始地址 
  4.                             InputIterator1 last1,       // 源容器1的终止地址 
  5.                             InputIterator2 first2,      // 源容器2的起始地址,元素个数与1相同 
  6.                             OutputIterator result,      // 目标容器的起始地址,元素个数与1相同 
  7.                             BinaryOperator binary_op ); // 函数指针 
  8. // typedef 目标容器元素类型 (*BinaryOperator)(源容器1元素类型,源容器2元素类型); 
  9. //*////////////////////////////////  

 

 

  1.   
  2. #include <iostream>  
  3. #include <algorithm>  
  4. #include <vector>  
  5. #include <string>  
  6. using namespace std;  
  7.   
  8. int op_increase (int i)  
  9. {  
  10.     return i+1;   
  11. }  
  12.   
  13. int op_sum (int i, int j)   
  14. {  
  15.     return i+j;   
  16. }  
  17.   
  18. int to_upper(int c)  
  19. {  
  20.     if (islower(c))  
  21.     {   
  22.         return (c-32);   
  23.     }  
  24.   
  25.     return c;  
  26. }  
  27.   
  28. int to_lower(int c)  
  29. {  
  30.     if (isupper(c))  
  31.     {  
  32.         return c+32;  
  33.     }  
  34.   
  35.     return c;  
  36. }  
  37.   
  38. int main () {  
  39.     vector<int> first;  
  40.     vector<int> second;  
  41.     vector<int>::iterator it;  
  42.       
  43.     // set some values:  
  44.     for (int i=1; i<6; i++) first.push_back (i*10); //  first: 10 20 30 40 50  
  45.       
  46.     ///将first容器的元素加1赋值给second容器  
  47.     second.resize(first.size());        // allocate space !!!必须预先设置一个大小与first相同  
  48.     transform (first.begin(), first.end(), second.begin(), op_increase); // second: 11 21 31 41 51  
  49.     cout << "second contains:";  
  50.     for (it=second.begin(); it!=second.end(); ++it)  
  51.     {  
  52.         cout << " " << *it;  
  53.     }  
  54.     cout << endl;  
  55.     //*////////////////////////////////////////////  
  56.       
  57.     ///将first容器的元素与second容器的元素相加,并将得到的结果重新赋值给first  
  58.     transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); //  first: 21 41 61 81 101  
  59.     cout << "first contains:";  
  60.     for (it=first.begin(); it!=first.end(); ++it)  
  61.         cout << " " << *it;  
  62.     cout << endl;  
  63.     //*//////////////////////////////////////////////////////////////////////////  
  64.   
  65.     ///大小写转换/////////////////////////////////////  
  66.     string strsrc("Hello, World!");  
  67.     string strdest;  
  68.     strdest.resize(strsrc.size());      // !!!必须预先设置一个大小与strsrc相同  
  69.     transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_upper); // 转换为大写  
  70.     cout << strdest << endl;  
  71.   
  72.     transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_lower); // 转换为小写  
  73.     cout << strdest << endl;  
  74.     //*/////////////////////////////////////////  
  75.   
  76.     return 0;  
  77. }  

posted on 2013-08-07 19:55  shoutcharter  阅读(165)  评论(0编辑  收藏  举报

导航