ptr_fun Function
単項および二項に適応できる関数に単項および二項な関数ポインターを、それぞれ、変換に使用されるヘルパー テンプレート関数。
template<class Arg, class Result>
pointer_to_unary_function<Arg, Result, Result (*)(Arg)>
ptr_fun(Result (*_pfunc)(Arg));
template<class Arg1, class Arg2, class Result>
pointer_to_binary_function<Arg1, Arg2, Result, Result (*)(Arg1, Arg2)>
ptr_fun(Result (*_pfunc)(Arg1, Arg2));
パラメーター
- _pfunc
アダプティブ可能な関数に変換する、単項演算子または二項関数ポインター。
戻り値
一つ目のテンプレート関数は、単項演算子関数 pointer_to_unary_function <Arg、[結果]> を返します (*) _pfunc。
2 番目のテンプレート関数の戻り値のバイナリ関数 pointer_to_binary_function <Arg1、Arg2[結果]、> (*) _pfunc。
解説
関数のオブジェクトで、関数をパラメーターとして使用する、適応性ではありませんが、標準テンプレート ライブラリのアルゴリズムに渡す。値を変数にバインドするか、拒否要素との使用などのアダプターで、使用するには、このような大量に使用する入れ子にされた型と指定する必要があります。ptr_fun のヘルパー関数での単項および二項な関数ポインターの変換は、関数のアダプターで単項および二項な関数ポインターを使用できるようになります。
使用例
// functional_ptr_fun.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <iostream>
int main( )
{
using namespace std;
vector <char*> v1;
vector <char*>::iterator Iter1, RIter;
v1.push_back ( "Open" );
v1.push_back ( "up" );
v1.push_back ( "the" );
v1.push_back ( "opalescent" );
v1.push_back ( "gates" );
cout << "Original sequence contains: " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; ++Iter1 )
cout << *Iter1 << " ";
cout << endl;
// To search the sequence for "opalescent"
// use a pointer_to_function conversion
RIter = find_if( v1.begin( ), v1.end( ),
not1 ( bind2nd (ptr_fun ( strcmp ), "opalescent" ) ) );
if ( RIter != v1.end( ) )
{
cout << "Found a match: "
<< *RIter << endl;
}
}
出力
Original sequence contains: Open up the opalescent gates
Found a match: opalescent
必要条件
ヘッダー : <functional>
名前空間: std