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);
}
![](https://img-my.csdn.net/uploads/201204/18/1334764395_7671.png)
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: "));
}
![](https://img-my.csdn.net/uploads/201204/25/1335320378_5039.png)
对于一般函数(非成员函数)可以使用prt_fun(op),ptr_fun允许在其他函数配接器中使用一般函数。
例如:pos = find_if(coll.begin(), coll.end(), not1(ptr_fun(check))); //bool check(int elem);