共用方式為


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

請參閱

參考

標準樣板程式庫