template <class S, class T> mem_fun_t<S,T> mem_fun (S (T::*f)()); template <class S, class T, class A> mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A)); template <class S, class T> const_mem_fun_t<S,T> mem_fun (S (T::*f)() const); template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A) const); |
template <class S, class T> mem_fun_t<S,T> mem_fun (S (T::*f)()) { return mem_fun_t<S,T>(f); } template <class S, class T, class A> mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A)) { return mem_fun1_t<S,T,A>(f); } template <class S, class T> const_mem_fun_t<S,T> mem_fun (S (T::*f)() const) { return const_mem_fun_t<S,T>(f); } template <class S, class T, class A> const_mem_fun1_t<S,T,A> mem_fun (S (T::*f)(A) const) { return const_mem_fun1_t<S,T,A>(f); } |
// mem_fun example #include <iostream> #include <functional> #include <vector> #include <algorithm> #include <string> using namespace std; int main () { vector <string*> numbers; // populate vector of pointers: numbers.push_back ( new string ("one") ); numbers.push_back ( new string ("two") ); numbers.push_back ( new string ("three") ); numbers.push_back ( new string ("four") ); numbers.push_back ( new string ("five") ); vector <int> lengths ( numbers.size() ); transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun(&string::length)); for (int i=0; i<5; i++) { cout << *numbers[i] << " has " << lengths[i] << " letters.\n"; } // deallocate strings: for (vector<string*>::iterator it = numbers.begin(); it!=numbers.end(); ++it) delete *it; return 0; } |
ptr_fun | 将函数指针转换为函数对象(类模板) |
mem_fun_ref | 将成员函数转换为函数对象(引用版本)(类模板) |
unary_function | 一元函数对象基类 (类模板) |
binary_function | 二元函数对象基类 (类模板) |