首页 > 其他 > 详细

transform 函数测试

时间:2015-03-11 19:38:06      阅读:219      评论:0      收藏:0      [点我收藏+]
#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

transform 函数测试

原文:http://www.cnblogs.com/sylar-liang/p/4330620.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!