STL仿函数functor

1.仿函数的妙处(P127)

示例:

//一般函数
void fo(){
	statements
}
//仿函数
class FunctionObjectType{
public:
	void operator() (){
		statements
	}
};

  • 仿函数比一般函数更灵巧。(可以拥有状态(state))
  • 每个仿函数都有其型别。导致:容器型别也会因为仿函数的不同而不同。
  • 执行速度上,仿函数通常比函数指针更快。

#include <list>
#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;

template <class T>
inline void PRINT_ELEMENTS(const T& coll, const char* optcstr=" ")
{
	typename T::const_iterator pos;
	std::cout<<optcstr;
	for(pos=coll.begin();pos!=coll.end();++pos){
		std::cout<<*pos<<' ';
	}
	std::cout<<std::endl;
}

//传值
class IntSequence{
private:
	int value;
public:
	//constructor
	IntSequence(int initialValue):value(initialValue){
	}
	//"function call"
	int operator() (){
		return value++;
	}
};

//传引用
class IntSequenceRef{
private:
	int& value;
public:
	//constructor
	IntSequenceRef(int& initialValue):value(initialValue){
	}
	//"function call"
	int operator() (){
		return value++;
	}
};

//传值和传引用模板类
template<typename T>
class IntSeque{
private:
	T value;
public:
	//constructor
	IntSeque(T initialValue):value(initialValue){
	}
	//"function call"
	int operator() (){
		return value++;
	}
};

int main()
{
	list<int> coll1;
	cout<<"coll1:(传值)"<<endl;
	generate_n(back_inserter(coll1),//start
		9,							//number of elements
		IntSequence(1));			//generates values
	PRINT_ELEMENTS(coll1);
	generate(++coll1.begin(),		//start
		--coll1.end(),				//end
		IntSequence(42));			//generate values
	PRINT_ELEMENTS(coll1);

	cout<<"coll2:(传引用)"<<endl;
	list<int> coll2;
	int value = 2;
	IntSequenceRef seq(value);
	generate_n (back_inserter(coll2),4,seq);
	PRINT_ELEMENTS(coll2);

	int k=10;
	generate_n(back_inserter(coll2),4,IntSequenceRef(k));
	PRINT_ELEMENTS(coll2);
	cout<<"k="<<k<<endl;
	generate_n(back_inserter(coll2),4,seq);
	PRINT_ELEMENTS(coll2);

	cout<<"coll3:(模板类)"<<endl;
	int refvalue=4;
	IntSeque<int&>	SeRef(refvalue);
	IntSeque<int>	Se(4);
	list<int>	coll3;
	generate_n(back_inserter(coll3),4,SeRef);
	PRINT_ELEMENTS(coll3);
	generate_n(back_inserter(coll3),4,SeRef);
	PRINT_ELEMENTS(coll3);
	generate_n(back_inserter(coll3),4,Se);
	PRINT_ELEMENTS(coll3);
	generate_n(back_inserter(coll3),4,Se);
	PRINT_ELEMENTS(coll3);
}

2.函数配接器

2.1含义:

指能够将仿函数和另一个仿函数(或某个值,或某个一般函数)结合起来的仿函数。声明于<functional>。函数配接器本身也是仿函数。

2.2针对成员函数而设计的函数配接器

mem_fun_ref(op) 调用op,某对象的一个const成员函数。
mem_fun(op) 调用op,某对象指针的一个const成员函数。
注:C++标准程序库并不针对non-const成员函数提供函数配接器。mem_fun_ref和mem_fun只能以无参数或单参数方式来调用成员函数。
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Person{
private:
	string name;
public:
	Person(string str):name(str){

	}
	void setperson(string n)
	{
		name=n;
	}
	void print() const{
		cout<<name<<endl;
	}
	void printwithprefix(string prefix) const{
		cout<<prefix<<name<<endl;
	}
};

int main()
{
	vector<Person> coll;
	Person p("0");
	for(int i=1;i<=8;++i)
	{
		string name(3,'a'+i);
		p.setperson(name);
		coll.push_back(p);
	}
	for_each(coll.begin(),coll.end(),mem_fun_ref(&Person::print));
	for_each(coll.begin(),coll.end(),bind2nd(mem_fun_ref(&Person::printwithprefix),"Person: "));
}


对于一般函数(非成员函数)可以使用prt_fun(op),ptr_fun允许在其他函数配接器中使用一般函数。

例如:pos = find_if(coll.begin(), coll.end(), not1(ptr_fun(check))); //bool check(int elem);

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值