STL函数对象及函数对象适配器

函数对象Functor

函数对象是重载了函数调用操作符的类的对象。因为这个对象重载了函数调用操作符,所以你可以像调用函数一样使用这个对象。

#include <iostream>
using namespace std;

class A
{
public:
 int operator() (int x,int y)
 {
  
  return x>y?x:y;
 }
};
void main()
{
 A a;
 cout<<a(1,2);
}

 

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来解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值