C++:transform用法


摘自:http://blog.csdn.net/luyuncheng/article/details/8659179View Code

 

 

    /*//////////////////////////////// 
    template < class InputIterator, class OutputIterator, class UnaryOperator > 
      OutputIterator transform ( InputIterator first1,  // 源容器的起始地址 
                                InputIterator last1,    // 源容器的終止地址 
                                OutputIterator result,  // 目標容器的起始地址 
                                UnaryOperator op );     // 函數指針 
    // typedef 目標容器元素類型 (*UnaryOperator)(源容器元素類型); 
     
    template < class InputIterator1, class InputIterator2, 
               class OutputIterator, class BinaryOperator > 
      OutputIterator transform ( InputIterator1 first1,     // 源容器1的起始地址 
                                InputIterator1 last1,       // 源容器1的終止地址 
                                InputIterator2 first2,      // 源容器2的起始地址,元素個數與1相同 
                                OutputIterator result,      // 目標容器的起始地址,元素個數與1相同 
                                BinaryOperator binary_op ); // 函數指針 
    // typedef 目標容器元素類型 (*BinaryOperator)(源容器1元素類型,源容器2元素類型); 
    //*////////////////////////////////  
      
    #include <iostream>  
    #include <algorithm>  
    #include <vector>  
    #include <string>  
    using namespace std;  
      
    int op_increase (int i)  
    {  
        return i+1;   
    }  
      
    int op_sum (int i, int j)   
    {  
        return i+j;   
    }  
      
    int to_upper(int c)  
    {  
        if (islower(c))  
        {   
            return (c-32);   
        }  
      
        return c;  
    }  
      
    int to_lower(int c)  
    {  
        if (isupper(c))  
        {  
            return c+32;  
        }  
      
        return c;  
    }  
      
    int main () {  
        vector<int> first;  
        vector<int> second;  
        vector<int>::iterator it;  
          
        // set some values:  
        for (int i=1; i<6; i++) first.push_back (i*10); //  first: 10 20 30 40 50  
          
        ///將first容器的元素加1賦值給second容器  
        second.resize(first.size());        // allocate space !!!必須預先設置一個大小與first相同  
        transform (first.begin(), first.end(), second.begin(), op_increase); // second: 11 21 31 41 51  
        cout << "second contains:";  
        for (it=second.begin(); it!=second.end(); ++it)  
        {  
            cout << " " << *it;  
        }  
        cout << endl;  
        //*////////////////////////////////////////////  
          
        ///將first容器的元素與second容器的元素相加,並將得到的結果重新賦值給first  
        transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); //  first: 21 41 61 81 101  
        cout << "first contains:";  
        for (it=first.begin(); it!=first.end(); ++it)  
            cout << " " << *it;  
        cout << endl;  
        //*//////////////////////////////////////////////////////////////////////////  
      
        ///大小寫轉換/////////////////////////////////////  
        string strsrc("Hello, World!");  
        string strdest;  
        strdest.resize(strsrc.size());      // !!!必須預先設置一個大小與strsrc相同  
        transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_upper); // 轉換為大寫  
        cout << strdest << endl;  
      
        transform(strsrc.begin(), strsrc.end(), strdest.begin(), to_lower); // 轉換為小寫  
        cout << strdest << endl;  
        //*/////////////////////////////////////////  
      
        return 0;  
    }  

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM