次の方法で共有


変換

ソース範囲内の各要素または要素のペアに対して、指定された関数のオブジェクトを 2 個のソース範囲内から、適用先の範囲オブジェクトに関数の戻り値をコピーします。

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
    アクティブにする最初のソース範囲内の先頭の要素の位置を示す入力反復子。

  • _Last1
    最初のソース範囲内の最後の要素の一つ前の 1 の位置を示す入力反復子は実行しました。

  • _First2
    アクティブにする二つ目のソースの範囲内の先頭の要素の位置を示す入力反復子。

  • _Result
    割り当て先範囲の先頭の要素の位置を示す出力反復子。

  • _Func
    2 種類のソース範囲内に、次の順序でペアに適用されるアルゴリズムの 2 番目の形式で使用された最初のソース範囲内または A User-Defined (UD) の二項関数オブジェクトの各要素に適用されるアルゴリズムの最初のバージョンで使用されるユーザー定義の単項関数オブジェクト。

戻り値

Output 要素を受け取っている割り当て先範囲の最後の要素の一つ前の位置 1 を示す出力反復子は、関数オブジェクトによって変換されます。

解説

参照される範囲が有効である必要があります。; すべてのポインターは dereferenceable、各シーケンス内で最後の位置は incrementation で最初から到達可能である必要があります。 割り当て先範囲は変換されたソース範囲内を格納するのに十分な大きさが必要です。

_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

参照

関連項目

標準テンプレート ライブラリ