Chinaunix首页 | 论坛 | 博客
  • 博客访问: 290605
  • 博文数量: 63
  • 博客积分: 814
  • 博客等级: 军士长
  • 技术积分: 700
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-09 15:46
文章分类

全部博文(63)

文章存档

2017年(1)

2016年(4)

2015年(13)

2014年(9)

2012年(3)

2011年(33)

分类: C/C++

2015-04-07 16:30:47

http://blog.sina.com.cn/s/blog_4e928b170100ukp2.html <原文>

在《Effective STL》一书中的解释是:所有重载了函数调用操作符(即operator())的类都是一个函数子类。从这些类创建的对象被称为函数对象或函数子,在STL中,大多数使用函数对象的地方同样也可以使用实际的函数。

就是说,函数子与函数对象是同一个概念。

另外这段话也说明,函数对象是一个类的实例,是一个类创建的对象,它和函数指针有着本质的差别。

 

以下内容转载自:http://www.cnblogs.com/ly4cn/archive/2007/07/21/826885.html

函数对象不是函数指针。但是,在程序代码中,它的调用方式与函数指针一样,后面加个括号就可以了。

函数对象实质上是一个实现了operator()--括号操作符--的类。

例如:

class Add
{
public:
int operator()(int a, int
 b)
{
return a +
 b;
}
};



Add add; // 定义函数对象
cout << add(3,2); // 5

函数指针版本就是:
int AddFunc(int a, int b)
{
return a +
 b;
}
typedef
 int (*Add) (int a, int b);

Add add = &AddFunc;
cout
 << add(3,2); // 5

呵呵,除了定义方式不一样,使用方式可是一样的。都是:
cout << add(3,2);

既然函数对象与函数指针在使用方式上没什么区别,那为什么要用函数对象呢?很简单,函数对象可以携带附加数据,而指针就不行了。
下面就举个使用附加数据的例子:
class less
{
public
:
less(
int
 num):n(num){}
bool operator()(int
 value)
{
return value <
 n;
}
private
:
int
 n;
};


使用的时候:
less isLess(10);
cout
 << isLess(9) << " " << isLess(12); //
 输出 1 0

这个例子好象太儿戏了,换一个:
const int SIZE = 5;
int array[SIZE] = { 50, 30, 9, 7, 20
};
// 找到小于数组array中小于10的第一个数的位置

int * pa = std::find_if(array, array + SIZE, less(10)); // pa 指向 9 的位置
// 找到小于数组array中小于40的第一个数的位置

int * pb = std::find_if(array, array + SIZE, less(40)); // pb 指向 30 的位置

这里可以看出函数对象的方便了吧?可以把附加数据保存在函数对象中,是函数对象的优势所在。
它的弱势也很明显,它虽然用起来象函数指针,但毕竟不是真正的函数指针。在使用函数指针的场合中,它就无能为力了。例如,你不能将函数对象传给qsort函数!因为它只接受函数指针。


要想让一个函数既能接受函数指针,也能接受函数对象,最方便的方法就是用模板。如:
template<typename FUNC>
int count_n(int* array, int size, FUNC func)
{
int count = 0
;
for(int i = 0; i < size; ++
i)
if
(func(array[i]))
count
 ++
;
return
 count;
}

这个函数可以统计数组中符合条件的数据个数,如:
const int SIZE = 5;
int array[SIZE] = { 50, 30, 9, 7, 20
};
cout
 << count_n(array, SIZE, less(10)); // 2

用函数指针也没有问题:
bool less10(int v)
{
return v < 10
;
}
cout
 << count_n(array, SIZE, less10); // 2

另外,函数对象还有一个函数指针无法匹敌的用法:可以用来封装类成员函数指针!
因为函数对象可以携带附加数据,而成员函数指针缺少一个类实体(类实例)指针来调用,因此,可以把类实体指针给函数对象保存起来,就可以用于调用对应类实体成员函数了。

template<typename O>
class memfun
{
public
:
memfun(
void(O::*f)(const char*), O*
 o): pFunc(f), pObj(o){}
void operator()(const char*
 name)
{
(pObj
->*
pFunc)(name);
}
private
:
void(O::*pFunc)(const char*
);
O
*
 pObj;
};

class
 A
{
public
:
void doIt(const char*
 name)
{ cout
 << "Hello " << name << "!"
;}
};


A a;
memfun
<A> call(&A::doIt, &a); // 保存 a::doIt指针以便调用

call("Kitty"); // 输出 Hello Kitty!

功告成了,终于可以方便保存成员函数指针,以备调用了。

不过,现实是残酷的。函数对象虽然能够保存成员函数指针和调用信息,以备象函数指针一样被调用,但是,它的能力有限,一个函数对象定义,最多只能实现一个指定参数数目的成员函数指针。
标准库的mem_fun就是这样的一个函数对象,但是它只能支持0个和1个参数这两种成员函数指针。如 int A::func()或void A::func(int)、int A::func(double)等等,要想再多一个参数如:int A::func(int, double),不好意思,不支持。想要的话,只有我们自已写了。
而且,就算是我们自已写,能写多少个?5个?10个?还是100个(这也太恐怖了)?
好在boost库提供了boost::function类,它默认支持10个参数,最多能支持50个函数参数(多了,一般来说这够用了。但它的实现就是很恐怖的:用模板部份特化及宏定义,弄了几十个模板参数,偏特化(编译期)了几十个函数对象。

----
C++0x已经被接受的一个提案,就是可变模板参数列表。用了这个技术,就不需要偏特化无数个函数对象了,只要一个函数对象模板就可以解决问题了。


http://blog.csdn.net/ozwarld/article/details/8252735

binder**和bind**功能对应。只是binder**是类绑定器,bind**为全局函数绑定器而已。

1st和2nd很好理解。一个是第一个参数不变,一个是第二个参数不变。

参看程序:


  1. #include "stdafx.h"  
  2.   
  3. #include   
  4. #include   // count_if  
  5. #include  // binder  
  6. #include   
  7.   
  8. using namespace std;  
  9.   
  10. int main()  
  11. {  
  12.     // Data  
  13.     int iarray[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  
  14.     list<int> aList(iarray, iarray + 10);  
  15.   
  16.     // binder和bind区别:  
  17.     // 1. 类绑定器有binder1st和binder2nd,而函数绑定器是bind1st和bind2nd  
  18.     // 2. bind是一个全局的模板函数其返回值为一个binder模板类的实例  
  19.     // 3. binder要指定泛型  
  20.     int k = count_if(aList.begin(), aList.end(), binder1stint>>(greater<int>(), 5));  
  21.   
  22.     // bind1st bind2nd功能比较  
  23.     // k = count_if(aList.begin(), aList.end(), bind1st(greater(), 5));   
  24.     // bind1st(greater(), 5); //---->5 > x 即5作为第一个固定参数。返回5大于的数字的个数  
  25.     // bind2nd(greater(), 5); //---->x > 5 即5作为第二个固定参。返回大于5的数字的个数  
  26.   
  27.     cout << k << endl;  
  28.   
  29.     system("pause");  
  30.     return 0;  
  31. }  

同理
#include 
#include 
#include 
#include 
using namespace std;

int main()
{
	vector coll;
	for(int i = 1; i <= 10; ++i)
	{
		coll.push_back(i);
	}
	//查找元素值大于10的元素的个数
	//也就是使得10 < elem成立的元素个数 
	int res = count_if(coll.begin(), coll.end(), bind1st(less(), 10));
	cout << res << endl;
	//查找元素值小于10的元素的个数
	//也就是使得elem < 10成立的元素个数 
	res = count_if(coll.begin(), coll.end(), bind2nd(less(), 10));
	cout << res << endl;
	
	return 0;
}

(转)解析bind1st和bind2nd的使用

http://www.cnblogs.com/crunchyou/archive/2012/11/09/2762178.html

阅读(922) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~