다음을 통해 공유


mem_fun Function

도우미 템플릿 함수 포인터 인수를 초기화 하는 경우 멤버 함수에 대해 함수 객체 어댑터를 구성 하는 데 사용 합니다.

template<class Result, class Type>
   mem_fun_t<Result, Type> mem_fun (
      Result(Type::* _Pm )( ) 
);

template<class Result, class Type, class Arg>
   mem_fun1_t<Result, Type, Arg> mem_fun(
      Result (Type::* _Pm )( Arg ) 
);

template<class Result, class Type>
   const_mem_fun_t<Result, Type>
      mem_fun(Result (Type::* _Pm )( ) const 
);

template<class Result, class Type, class Arg>
   const_mem_fun1_t<Result, Type, Arg>
      mem_fun(Result (Type::* _Pm )( Arg ) const 
);

매개 변수

  • _Pm
    클래스의 멤버 함수에 대 한 포인터 유형 함수 개체를 변환할 수 있습니다.

반환 값

A const 또는 non_const 함수 개체의 형식 mem_fun_t 또는 mem_fun1_t.

예제

// functional_mem_fun.cpp
// compile with: /EHsc
#include <vector>
#include <functional>
#include <algorithm>
#include <iostream>

using namespace std;

class StoreVals   
{
    int val;
public:
    StoreVals() { val = 0; }
    StoreVals(int j) { val = j; }

    bool display() { cout << val << " "; return true; }
    int squareval() { val *= val; return val; }
    int lessconst(int k) {val -= k; return val; }
};

int main( )
{
    vector<StoreVals *> v1;

    StoreVals sv1(5);
    v1.push_back(&sv1);
    StoreVals sv2(10);
    v1.push_back(&sv2);
    StoreVals sv3(15);
    v1.push_back(&sv3);
    StoreVals sv4(20);
    v1.push_back(&sv4);
    StoreVals sv5(25);
    v1.push_back(&sv5);

    cout << "The original values stored are: " ;
    for_each(v1.begin(), v1.end(), mem_fun<bool, StoreVals>(&StoreVals::display));
    cout << endl;

    // Use of mem_fun calling member function through a pointer
    // square each value in the vector using squareval ()
    for_each(v1.begin(), v1.end(), mem_fun<int, StoreVals>(&StoreVals::squareval));   
    cout << "The squared values are: " ;
    for_each(v1.begin(), v1.end(), mem_fun<bool, StoreVals>(&StoreVals::display));
    cout << endl;

    // Use of mem_fun1 calling member function through a pointer
    // subtract 5 from each value in the vector using lessconst ()
    for_each(v1.begin(), v1.end(), 
        bind2nd (mem_fun1<int, StoreVals,int>(&StoreVals::lessconst), 5));   
    cout << "The squared values less 5 are: " ;
    for_each(v1.begin(), v1.end(), mem_fun<bool, StoreVals>(&StoreVals::display));
    cout << endl;
}

Output

The original values stored are: 5 10 15 20 25 
The squared values are: 25 100 225 400 625 
The squared values less 5 are: 20 95 220 395 620 

요구 사항

헤더: <functional>

네임 스페이스: std

참고 항목

참조

표준 템플릿 라이브러리