transform、for_each

transform

  与for_each类似可搭配function object对range内的元素进行操作,但for_each不关心function object的返回值,但transform会把操作后的range输出到另一个range,可以被就地来更改序列,允许first和result相等,但outputIterator result不可与first和last中的任何一个inputIterator相同

//版本一:UnaryFunction作用于input range上, 
template <class InputerIterator,class OutputIterator,class UnaryFunction>
OutputIterator transform(InputerIterator first,InputerIterator last,OutputIterator result,UnaryFunction cmp);

//版本二:第一个range对应的i1和第二个range对应的i2,cmp(*i1,*i2)后赋值给result 
template <class InputerIterator1,class InputerIterator2,class OutputIterator,class BinaryFunction>
OutputIterator transform(InputerIterator first1,InputerIterator last1,
                         InputerIterator2 first2,first2OutputIterator result,BinaryFunction cmp);

for_each

  将函数f施加于区间[first,last)的每一个元素身上。

template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, Function f)
{
    for ( ; first!=last; ++first ) f(*first);
    return f;
}
原文地址:https://www.cnblogs.com/tianzeng/p/10403834.html