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