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;
}

输出




原文地址:https://www.cnblogs.com/jinxiang1224/p/8468428.html