zoukankan      html  css  js  c++  java
  • transform 函数测试

    #include <iostream>
    #include <vector>
    #include <set>
    #include <algorithm>
    using namespace std;
    
    
    // 自定义泛函数
    template <class T>
    void PRINT_ELEMENTS(const T& coll, const char * str="")
    {
        typename T::const_iterator pos;
        
        cout<<str<<endl;
        for(pos = coll.begin(); pos != coll.end(); ++pos)
        {
            cout<<*pos<<" ";    
        }
        cout<<endl;
    }
    
    
    // 以函数作为算法的参数
    int square(int data)
    {
        return data*data;    
    }
    
    
    int main(int argc, char** argv)
    {
        vector<int> coll;
        
        set<int> IntSet;
        for(int i=1; i<11; ++i)
        {
            IntSet.insert(i);    
        }
        PRINT_ELEMENTS(IntSet, "Set original data:");
        
        std::transform(IntSet.begin(), IntSet.end(), // source 源地址
                                    std::back_inserter(coll), // destination 目标地址
                                    square); // 以函数作为算法的参数
        PRINT_ELEMENTS(coll, "after square:");
    
        return 0;    
    }

    结果输出:

    Set original data:
    1 2 3 4 5 6 7 8 9 10
    after square:
    1 4 9 16 25 36 49 64 81 100

  • 相关阅读:
    面向对象的设计模式2
    数据结构
    算法题目1
    5.7(1) 反射
    hashMap原理(java8)
    6.1 接口
    18.1 线程
    13.2 具体的集合
    scrapy(2)——scrapy爬取新浪微博(单机版)
    5.1 类、超类和子类
  • 原文地址:https://www.cnblogs.com/sylar-liang/p/4330620.html
Copyright © 2011-2022 走看看