次の方法で共有


for_each

範囲内で前方順序で各要素に対して、指定された関数を適用し、オブジェクトの関数オブジェクトを返します。

template<class InputIterator, class Function> 
   Function for_each( 
      InputIterator _First,  
      InputIterator _Last,  
      Function _Func 
   );

パラメーター

  • _First
    アクティブにする範囲の先頭の要素の位置を示す入力反復子。

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

  • _Func
    範囲内の各要素に適用されるユーザー定義関数オブジェクト。

戻り値

span 要素すべてに適用すると、Function オブジェクトのコピー。

解説

アルゴリズム for_each は非常に柔軟性があり、さまざまな、ユーザー指定の方法で範囲内の各要素の変更を許可します。 Templatized 関数は変更されたフォームにパラメーターを渡すことにより再利用される場合があります。 ユーザー定義関数は、アルゴリズムが span 要素をすべて処理後に返される可能性がある内部状態内の情報を収集することがあります。

参照される範囲が有効である必要があります。; すべての dereferenceable ポインターは、シーケンス内の最後の位置は incrementation で最初から到達可能である必要があります。

複雑に_Last  (– _First) 比較と最大で直線的です。

使用例

// alg_for_each.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#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
   void operator ( ) ( Type& elem ) const
   {
      elem *= Factor;
   }
};

// The function object to determine the average
class Average
{
private:
   long num;      // The number of elements
   long sum;      // The sum of the elements
public:
   // Constructor initializes the value to multiply by
   Average ( ) : num ( 0 ) , sum ( 0 )
   {
   }

   // The function call to process the next elment
   void operator ( ) ( int elem ) \
   {
      num++;      // Increment the element count
      sum += elem;   // Add the value to the partial sum
   }

   // return Average
   operator double ( )
   {
      return  static_cast <double> (sum) /
      static_cast <double> (num);
   }
};

int main( )
{
   using namespace std;
   vector <int> v1;
   vector <int>::iterator Iter1;

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

   // Using for_each to multiply each element by a Factor
   for_each ( v1.begin ( ) , v1.end ( ) , MultValue<int> ( -2 ) );

   cout << "Multiplying the elements of the vector v1\n "
        <<  "by the factor -2 gives:\n v1mod1 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // The function object is templatized and so can be
   // used again on the elements with a different Factor
   for_each (v1.begin ( ) , v1.end ( ) , MultValue<int> (5 ) );

   cout << "Multiplying the elements of the vector v1mod\n "
        <<  "by the factor 5 gives:\n v1mod2 = ( " ;
   for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
      cout << *Iter1 << " ";
   cout << ")." << endl;

   // The local state of a function object can accumulate
   // information about a sequence of actions that the
   // return value can make available, here the Average
   double avemod2 = for_each ( v1.begin ( ) , v1.end ( ) ,
      Average ( ) );
   cout << "The average of the elements of v1 is:\n Average ( v1mod2 ) = "
        << avemod2 << "." << endl;
}
  

必要条件

ヘッダー: <algorithm>

名前空間: std

参照

関連項目

for_each (STL のサンプル)

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