• C++std函数之transform


    /*//////////////////////////////// 
        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;  
        }
  • 相关阅读:
    微信小程序 登录
    小程序验证码输入框 连续输入 自动跳到下一个input
    微信小程序支付方法
    判断屏幕是否过大 或国小 进行 缩放达到自适应
    reactnative 启动
    第二届中国云计算应用论坛圆满闭幕 北达软
    什么是EA? 北达软
    第二届中国云计算应用论坛2012.01.08北京大学隆重揭幕 北达软
    北达软主办的企业架构与数据规划培训圆满结束 北达软
    北达软主办的“第九期中国EA沙龙”圆满举行 北达软
  • 原文地址:https://www.cnblogs.com/djzny/p/4484115.html
Copyright © 2020-2023  润新知