共用方式為


ptr_fun 函式

Helper 用於樣板函式轉換一元和二元函式指標,分別,輸入一元和二元適應性函式。

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 <函式ArgResult>* (_pfunc)。

第二個樣板函式會傳回二進位函式 pointer_to_binary_function <Arg1Arg2Result>* (_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;
    }
}

Output

Original sequence contains: Open up the opalescent gates
Found a match: opalescent

需求

標題: <functional>

命名空間: std

請參閱

參考

標準樣板程式庫