共用方式為


transform

將指定的函式物件至來源中的每個項目或對一組從兩個來源範圍的元素複製到函式物件的傳回值到目的範圍。

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

參數

  • _First1
    輸入定址的 Iterator 第一個項目的位置在第一個來源範圍起作用。

  • _Last1
    輸入定址的 Iterator 將最後一個項目的位置會在第一個來源範圍起作用。

  • _First2
    輸入定址的 Iterator 第一個項目的位置在第二個來源範圍起作用。

  • _Result
    輸出定址的 Iterator 第一個項目位置的範圍。

  • _Func
    用來套用至用於演算法第二版的第一個來源範圍或由使用者定義的 (UD) 二元函式物件的每個元素依轉寄順序配對,套用,對兩個來源範圍演算法的第一個使用者定義之一元的函式物件。

傳回值

輸出定址的 Iterator 將最後一個項目的位置。在接收輸出之項目的目的範圍由函式物件轉換為。

備註

參考的範圍必須是有效的;所有指標必須 dereferenceable,並且每個序列中的最後位置必須取用開頭 (由增量。 目的範圍必須足以包含已轉換的來源範圍。

如果 _Result 等於在演算法的第一個版本的 _First1*,* 則來源和目的範圍將是相同的,並將就地修改序列。 但 _Result 無法解決在這個範圍 [_First1 +1, _Last1) 內的位置。

複雜是線性與至多 (_Last1 – _First1) 比較。

範例

// alg_transform.cpp
// compile with: /EHsc
#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;
}
  

需求

標頭:<algorithm>

命名空間: std

請參閱

參考

標準樣板程式庫