C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

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

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.cppblog.com/hdqqq/archive/2006/09/13/12424.aspx

同时参考msdn:http://msdn2.microsoft.com/en-us/library/4y7z5x4b(VS.80).aspx
 

 

 

posted on 2007-08-14 11:37 梦在天涯 阅读(7227) 评论(2)  编辑 收藏 引用 所属分类: CPlusPlus

评论

# re: STL函数对象及函数对象适配器[未登录] 2007-08-14 13:32 flyman

not1 //对一元的函数对象取反
not2 //对二元的函数对象取反

对对象取反好像不太恰当:

函数对象的OPERATOR()返回值取反。


不错!继续。  回复  更多评论   

# re: STL函数对象及函数对象适配器 2008-06-06 14:46 bneliao

讲解很清晰  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1783933
  • 排名 - 5

最新评论

阅读排行榜