标准库transform()函数

transform知识点小结

一、来源与功能

来自<algorithm>头文件,对序列进行变换操作

二、使用方式

  • unary operation(1)
template <class InputIterator, class OutputIterator, class UnaryOperation>
  OutputIterator transform (InputIterator first1, InputIterator last1,
                            OutputIterator result, UnaryOperation op);
  • binary operation(2)
template <class InputIterator1, class InputIterator2,
          class OutputIterator, class BinaryOperation>
  OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
                            InputIterator2 first2, OutputIterator result,
                            BinaryOperation binary_op);

三、参数解释

  • first1, last1:第一个序列的元素范围[first1,last1)
  • first2:第二个序列的元素范围[first2, first2+last1-first1)
  • op:一元操作符
  • binary_op:二元操作符,可以是函数指针或函数对象

四、返回值

指向结果序列最后一个元素的下一个位置

五、示例程式

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

namespace sxg00 {
    void printSeq(vector<int>& vec) {
        for (int i = 0; i < vec.size(); i++) {
            cout << vec[i] << " ";
        }
        cout << '\n';
    }

    int op_increase(int i) {
        return ++i;
    }
    void test_transform() {
        vector<int> source;

         for (int i=1; i<6; i++)
            source.push_back (i*10);  

        vector<int> des;
        des.resize(source.size());

        // test transform Unary operation

        /**
         unary operation(1) 
        template <class InputIterator, class OutputIterator, class UnaryOperation>
            OutputIterator transform (InputIterator first1, InputIterator last1,
                                  OutputIterator result, UnaryOperation op);
         */
        vector<int>::iterator it = transform(source.begin(), source.end(), des.begin(), op_increase);
        cout << *(--it) << endl;
        cout << "des contains:";
        printSeq(des);



        // test transform Binary operation
        /**
         binary operation(2)    
    template <class InputIterator1, class InputIterator2,
          class OutputIterator, class BinaryOperation>
                OutputIterator transform (InputIterator1 first1, InputIterator1 last1,
                                          InputIterator2 first2, OutputIterator result,
                                          BinaryOperation binary_op);
         */
        transform(source.begin(), source.end(), des.begin(), source.begin(), std::plus<int>());
        cout << "source contains:";
        printSeq(source);
    }
}

int main() {
    sxg00::test_transform();
    return 0;
}

输出结果

51
des contains:11 21 31 41 51
source contains:21 41 61 81 101

参考链接

http://www.cplusplus.com/reference/algorithm/transform/?kw=transform

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 理解SVG坐标系和变换(第二部分)transform (本文转自w3cplus,这里仅修正了部分个人认为翻译不恰当...
    王策北阅读 2,187评论 0 1
  • 关于css3变形 CSS3变形是一些效果的集合,比如平移、旋转、缩放和倾斜效果,每个效果都被称作为变形函数(Tra...
    hopevow阅读 11,483评论 2 13
  • 第一部分:变形介绍 自层叠样式表诞生以来,元素始终是矩形的,而且只能沿着横轴和纵轴放置。有些技巧能让元素看起来是倾...
    侠客有情剑无情QAQ阅读 5,405评论 0 17
  • CSS3动画的属性主要分为三类:transform、transition以及animation。 Transfor...
    may_mico阅读 14,121评论 1 19
  • CSS里transform变形这个属性有点学习难度,尤其在CSS3里加上了3D效果之后,2维变3维学习成本更是成倍...
    BULL_DEBUG阅读 4,375评论 0 1