• transform算法


    功能

          transform算法提供两种能力:

    1.第一种形式有4个参数,把源区间的元素转换到目标区间。

    2.第二种形式有5个参数,将两个源序列中的元素合并,并将结果写入目标区间。

    公共函数

    //模板参数T的类型由PrintElements调用的实参推断得出
    template<class T>
    void PrintElements(const T& coll, const char* optcstr = "")
    {
    
    	typename T::const_iterator pos;
    	std::cout << optcstr;
    	for (pos = coll.begin(); pos != coll.end(); ++pos)
    	{
    		std::cout << *pos << ' ';
    	}
    
    	std::cout << std::endl;
    }
    //模板参数T的类型由InsertElements调用的实参推断得出
    template<class T>
    void InsertElements(T& coll, const unsigned short usBegin,  const unsigned short usEnd)
    {
    	T::iterator it = coll.begin();
    	int i = 0;
    
    	for (i = usBegin; i <= usEnd; ++i)
    	{
    		it = coll.insert(it, i);
    		++it;
    	}
    }

    接口

    形式一

    template<class InputIterator, class OutputIterator, class UnaryFunction>
    OutputIterator transform(
          InputIterator _First1, 
          InputIterator _Last1, 
          OutputIterator _Result,
          UnaryFunction _Func
          );

    说明:

    • 针对源区间[_First1, _Last1)中的每个元素调用:_Func(elem),并将结果写到以_Result起始的目标区间内。
    • 返回目标区间内“最后一个被转换元素”的下一个位置。
    • 调用这保证目标区间内有足够空间元素,否则需要使用插入型迭代器,比如back_inserter()。
    • _First1和_Result可以相同。
    • 复杂度:线性,对_Func执行numberOfElements次调用。

    实例 

    #include "stdafx.h"
    #include <string.h>
    #include <algorithm>
    #include <vector>
    #include <deque>
    #include <functional>
    #include <iostream>
    #include <list>
    #include <sstream>
    #include <iterator>
    #include <functional>
    #include <stdlib.h>
    #define  MAX_NUM 10
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	vector<int> vecCollection;
    	list<int> lsIntCollection;
    
    	InsertElements(vecCollection, 1, 9);//1 2 ... 9
    	PrintElements(vecCollection, "vecCollection: ");
    
    	//transforms算法的第一种形式:
    	//negate all elements in vecCollection
    	transform(vecCollection.begin(), vecCollection.end(), //source range
    			  vecCollection.begin(),					  //destination range
    			  negate<int>()								  //operation
    			  );
    	PrintElements(vecCollection, "element negated: ");
    	
    	//all elements multiply 10
    	transform(vecCollection.begin(), vecCollection.end(),	//source range
    			  back_inserter(lsIntCollection),				//destination range
    			  bind2nd(multiplies<int>(), 10)				//operation
    			  );
    	PrintElements(lsIntCollection, "element * 10: ");
    	
    	//print lsIntCollection negatively and in reverse order
    	cout << "negate all elements and print in reverse order:" << endl;
    	transform(lsIntCollection.rbegin(), lsIntCollection.rend(),	//source range
    			  ostream_iterator<int>(cout, " "),					//destination range
    			  negate<int>()										//operation
    			  );
    
    	cout << endl;
    
    	return 0;
    }

    输出:


    形式二

    template<class InputIterator1, class InputIterator2, class OutputIterator,
    		class BinaryFunction>
    OutputIterator transform(
          InputIterator1 _First1, 
          InputIterator1 _Last1,
          InputIterator2 _First2, 
          OutputIterator _Result,
          BinaryFunction _Func
       );

    说明

    • 针对第一个源区间[_First1, _Last1)以及以_First2开始的第二个源区间的对应元素,调用_Func(Source1elem,Source2elem);
    • 返回目标区间内“最后一个被转换元素”的下一个位置。
    • 调用者必须保证第二个源区间的元素个数不会比第一个源区间元素小,至少和第一个源区间有相同大小的元素。
    • _First1,_First2以及_Result可以相同,所以,,可以让元素自己和自己结合,然后将结果覆盖到某个序列。
    • 复杂度:线性;对_Func执行numberOfElements次调用。


    实例

    #include "stdafx.h"
    #include <string.h>
    #include <algorithm>
    #include <vector>
    #include <deque>
    #include <functional>
    #include <iostream>
    #include <list>
    #include <sstream>
    #include <iterator>
    #include <functional>
    #include <stdlib.h>
    #define  MAX_NUM 10
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	
    	vector<int> vecCollection;
    	list<int> lsIntCollection;
    
    	InsertElements(vecCollection, 1, 9);//1 2 ... 9
    	PrintElements(vecCollection, "vecCollection: ");	
    
    	//transforms算法的第二种形式
    	//square each elements
    	transform(vecCollection.begin(), vecCollection.end(),		//first source range
    			  vecCollection.begin(),							//second source range
    			  vecCollection.begin(),							//destination range
    			  multiplies<int>()									//operation
    			  );
    	PrintElements(vecCollection, "vecCollection squared:");
    
    	//add each element and insert result to lsIntCollection
    	transform(vecCollection.begin(), vecCollection.end(),		//first source range
    			  vecCollection.begin(),							//second source range
    			  back_inserter(lsIntCollection),					//destination range
    			  plus<int>()										//operation
    			  );
    	PrintElements(lsIntCollection, "vecCollection plus by self:");
    
    	return 0;
    }

    输出




  • 相关阅读:
    php内存管理机制与垃圾回收机制
    PHP Laravel5实现的RBAC权限管理操作示例
    PHP实现微信企业付款到个人零钱步骤
    ThinkPHP 6.0 管道模式与中间件的实现分析
    深入讲解 Laravel 的 IoC 服务容器
    ThinkPHP6 核心分析:系统服务
    PHP 性能优化
    PHP 7.4 新语法:箭头函数
    深入理解 PHP 的 7 个预定义接口
    Java实现 LeetCode 795 区间子数组个数 (暴力分析)
  • 原文地址:https://www.cnblogs.com/jinxiang1224/p/8468428.html
Copyright © 2020-2023  润新知