transform的使用方法

本文详细介绍了STL中的transform函数,包括一元操作符和二元操作符两种形式,并通过具体的代码示例展示了如何使用该函数对容器中的元素进行运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

transform的功能,对[first1,first2]中的元素进行运算,并把结果存储在result中

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

 

transform有两种形式,一种是对一元操作符,另外一种是二元操作符。

一元操作符的情形如下:

template <class InputIterator, classOutputIterator, class UnaryOperator>

 OutputIterator transform (InputIterator first1, InputIterator last1,

                            OutputIteratorresult, UnaryOperator op)

{

  while (first1 != last1) {

   *result = op(*first1);  // or: *result=binary_op(*first1,*first2++);

   ++result; ++first1;

  }

  return result;

}


二元操作符时,各iterator的含义:

二元操作符时,函数意思是,first1 中的元素和first2中的元素进行op运算,存储到result中

实例说明:

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

// The function object multiplies an element by a Factor
template <class Type>
class MultValue
{
   private:
      Type Factor;   // The value to multiply by
   public:
      // Constructor initializes the value to multiply by
      MultValue ( const Type& _Val ) : Factor ( _Val ) {
      }

      // The function call for the element to be multiplied
      Type operator ( ) ( Type& elem ) const 
      {
         return elem * Factor;
      }
};

int main( )
{	 
   using namespace std;
   vector <int> v1, v2 ( 7 ), v3 ( 7 );
   vector <int>::iterator Iter1, Iter2 , Iter3;

   // Constructing vector v1
   int i;
   for ( i = -4 ; i <= 2 ; i++ )
   {
      v1.push_back(  i );
   }

   cout << "Original vector  v1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Modifying the vector v1 in place
   transform (v1.begin ( ) , v1.end ( ) , v1.begin ( ) , MultValue<int> ( 2 ) );
   cout << "The elements of the vector v1 multiplied by 2 in place gives:"
        << "\n v1mod = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // Using transform to multiply each element by a factor of 5
   transform ( v1.begin ( ) , v1.end ( ) , v2.begin ( ) , MultValue<int> ( 5 ) );
   
   cout << "Multiplying the elements of the vector v1mod\n "
        <<  "by the factor 5 & copying to v2 gives:\n v2 = ( " ;
   for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
      cout << *Iter2 << " ";
   cout << ")." << endl;

   // The second version of transform used to multiply the 
   // elements of the vectors v1mod & v2 pairwise
   transform ( v1.begin ( ) , v1.end ( ) ,  v2.begin ( ) , v3.begin ( ) , 
      multiplies <int> ( ) );
   
   cout << "Multiplying elements of the vectors v1mod and v2 pairwise "
        <<  "gives:\n v3 = ( " ;
   for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
      cout << *Iter3 << " ";
   cout << ")." << endl;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值