STL函數對象及函數對象適配器
一 函數對象Functor
STL中提供了一元和二元函數的兩種Functor,通過unary_function和binary_function提供了這兩種不同參數數量的Functor的基本結構,在這兩個類型中,分別內嵌定義一元和二元函數操作在模版推演的時候需要用到的typedef.
//一元函數的定義為
template<class _A, class _R>
struct unary_function {
typedef _A argument_type;
typedef _R result_type;
};
//二元函數的定義為
template<class _A1, class _A2, class _R>
struct binary_function {
typedef _A1 first_argument_type;
typedef _A2 second_argument_type;
typedef _R result_type;
};
其他的一元和二元Functor可以從這兩個基本結構繼承,同時也就可以推演出函數的參數和返回值的類型,STL在上述這兩個結構的基礎上,實現了很多一元和二元的Functor.
//一元
negate
//二元
plus
minus
multiplies
divides
modulus
equal_to
not_equal_to
greater
greater_equal
less
less_equal
logical_and
logical_or
logical_not
上面的這些Functor都是基于模版實現的,可以象下面那樣使用的方式:
plus<int> int_plus;
cout << int_plus(111,222) << endl;
二 函數對象適配器
函數對象適配器的作用就是使函數轉化為函數對象,或是將多參數的函數對象轉化為少參數的函數對象。
1)bind
bind1st //通過綁定第一個參數,使二元的函數對象轉化為一元的函數對象
bind2nd //通過綁定第二個參數,使二元的函數對象轉化為一元的函數對象
not1 //對一元的函數對象取反
not2 //對二元的函數對象取反
使用的方式:
bind1st( less<int>(), 10)(20);
not2( less<int() )(10,20);
這些Functor看起來好像好像用處不大,但是在和STL中的容器和算法結合在一起使用的時候,就會使得程序顯得很簡潔.
int i;
vector<int> lv;
for(i = 0; i < 100; i++)
{
lv.push_back(i);
}
//對vector中小于20的數進行記數
cout << count_if(lv.begin(), lv.end(), bind2nd(less<int>(), 20)) << endl;
//由大到小排序
sort(lv.begin(), lv.end(), not2(less<int>()) ) ;
for (i = 0; i < 100; i++)
{
cout << lv.at(i) << endl;
}
2)ptr_fun
ptr_fun是指將現有的函數轉換為Functor的功能.在STL中提供了這個功能的Functor,就是pointer_to_unary_function和pointer_to_binary_function這兩個類,這兩個類對應一元
和二元兩種函數,也就是說,對于調用參數為3個或者多于3個的函數,STL提供的Functor類,無法配接.
基本使用方法:
int u_func(int a)
{
int ret = a;
return ret;
}
int b_func(int a,int b)
{
return a+b;
}
void call()
{
pointer_to_unary_function<int,int> uf(u_func);
cout << uf(100) << endl;
pointer_to_binary_function<int,int,int> bf(b_func);
cout << bf(111,222) << endl;
//或者
cout << ptr_fun(u_func)(100) << endl;
cout << ptr_fun(b_func)(111,222) << endl;
}
可以看到,上面的方法改進了原先C和C++中通過函數指針來間接調用函數的方法,將函數指針封裝到了類中.
問題:
第一部分中的Functor中是自己定義操作符(),但是在ptr_fun中,是將已經有的function轉為Functor調用就會存在一個調用方式的問題.
c++中的函數,按調用方式可以分為__cdecl, __stdcall,__fastcall 三種,ptr_fun如何正確的識別給定的function的調用方式就會有問題.
其中:
vc6中的STL的ptr_fun代碼中,統一將function認為是__cdecl調用方式. 而Dev-cpp中使用的SGI的代碼中沒有明確指明函數的調用方式,所以將使用編譯器的確省設置.
但是如果將上面的b_func函數改為
int __stdcall b_func(int a,int b)
{
return a+b;
}
上面的使用代碼在DEV-CPP中無法編譯通過.
3)mem_fun
mem_fun是將某個類中的成員函數轉變為Functor的功能.
一般的使用方法
struct mem_fun_struct
{
int n_mem_fun() {
cout << "mem_fun_struct::n_mem_fun()" << endl;
return 0;
}
int u_mem_fun(int a) {
cout << "mem_fun_struct::u_mem_fun(int) " << a << endl;
return a;
}
int b_mem_fun(int a,int b) {
cout << "mem_fun_struct::b_mem_fun(int,int)" << a << " " << b << endl;
return a+b;
}
};
void call()
{
mem_fun_struct ls;
mem_fun(&mem_fun_struct::n_mem_fun)(&ls);
mem_fun(&mem_fun_struct::u_mem_fun)(&ls, 10);
//mem_fun(&mem_fun_struct::u_mem_fun)(&ls, 10, 20);
}
上面的代碼在dev-cpp 4.9.9中編譯通過,SGI STL中沒有提供二元成員函數的mem_fun,vc6中提供了mem_fun(無參數成員函數)和mem_fun1(一元參數成員函數), 而在vs2003中改變了用法.但是我看MSDN好像也只支持到一個參數.
三 總結
STL中提供了基本的一元和二元參數的Functor, 同時提供了相應的適配器可以對Functor進行修飾,Functor可以很好的和 STL容器,STL算法結合使用.
但是仍有問題:
1)上面說到的調用方式
2) 多參數函數對象適配
對于我們比較復雜的stl不能滿足要求的問題,我們可以是用boost或loki來解決。
四 參考
本文基本是對hdqqq的文章轉載,稍加整理!原文地址:http://www.shnenglu.com/hdqqq/archive/2006/09/13/12424.aspx
同時參考msdn:http://msdn2.microsoft.com/en-us/library/4y7z5x4b(VS.80).aspx