函数适配器
函数适配器概念
STL中定义了大量的函数对象,但是有时候需要对函数返回值进行进一步的简单计算,或者填上多余的参数,不能直接代入算法,函数适配器实现了这一功能,将一种函数对象转化为另一种符合要求的函数对象,函数适配器可以分为4大类,绑定适配器,组合适配器,指针函数适配器和成员函数适配器
直接构造STL中的函数适配器通常会导致冗长的类型声明。为简化安徽念书适配器的构造,STL还提供了函数适配器辅助函数,借助于泛型自动推断技术,无须显式的类型声明便可实现函数适配器的构造
常用函数函数适配器
标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象。常用适配器是: 1 绑定器(binder):binder 通过把二元函数对象的一个实参绑定到一个特殊的值上,将其转 换成一元函数对象。C++标准库提供两种预定义的 binder 适配器:bind1st 和 bind2nd,前 者把值绑定到二元函数对象的第一个实参上,后者绑定在第二个实参上。 2 取反器(negator):negator 是一个将函数对象的值翻转的函数适配器。标准库提供两个预定 义的 ngeator 适配器:not1 翻转一元预定义函数对象的真值,而 not2 翻转二元谓词函数的真 值。
常用函数适配器列表如下:
-
bind1st(op,value)
-
bind2nd(op,value)
-
not1(op)
-
not2(op)
-
mem_fun_ref(op)
-
mem_fun(op) ptr_fun(op)
#include<iostream> using namespace std; #include<vector> #include<algorithm> #include<functional> class MyPrint:public binary_function<int,int,void> { public: void operator()(int v, int start) const { cout << "v=" << v << "start=" << start << "v+start=" << v + start << endl; } }; void test01() { vector<int>v; for (int i = 0; i < 10; i++) { v.push_back(i); } cout << "请输入一个起始值:" << endl; int num; cin >> num; //for_each(v.begin(), v.end(), bind2nd (MyPrint(),num)); for_each(v.begin(), v.end(), bind1st(MyPrint(), num)); } //第一步,绑定数据 bind2nd //第二步,继承类 binary_function<参数类型1,参数类型2,返回值类型> //第三步,加const修饰operator() int main() { test01(); system("pause"); return 0; }
取反适配器
class MyPrint:public binary_function<int,int,void>
{
public:
void operator()(int v, int start) const
{
cout << "v=" << v << "start=" << start << "v+start=" << v + start << endl;
}
};
//取反适配器
class CreateThenFive:public unary_function<int,bool>
{
public:
bool operator()(int v)const
{
return v > 5;
}
};
void test02()
{
//一元取反
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找大于5的数字
//需求改为找小于5的数字
//vector<int>::iterator pos=find_if(v.begin(), v.end(),not1 (CreateThenFive()));
vector<int>::iterator pos=find_if(v.begin(), v.end(),not1(bind2nd(greater<int>(),5)));
if (pos != v.end())
{
cout << "找到大于5的数字为:" <<*pos<< endl;
}
else
{
cout << "未找到" << endl;
}
}
//一元取反适配器 not1
//继承unary_fuction<类型1,返回值类型>
//const
函数指针适配器
void MyPrint03(int v,int start)
{
cout << v+start << endl;
}
//函数指针适配器
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//将函数指针 适配为函数对象
//ptr_fun
for_each(v.begin(), v.end(),bind2nd (ptr_fun (MyPrint03),100));
}
成员函数适配器
//成员函数适配器
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
void showPerson()
{
cout << "成员函数中姓名:" << m_Name << "年龄:" << m_Age << endl;
}
void plusAge()
{
this->m_Age+=100;
}
string m_Name;
int m_Age;
};
void MyPrintPerson(Person &p)
{
cout << "姓名:" << p.m_Name << "年龄:" << p.m_Age << endl;
}
void test04()
{
vector<Person >v;
Person p1("aaa", 10);
Person p2("bbb", 15);
Person p3("ccc", 18);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//成员函数适配器
//mem_fun_ref
for_each(v.begin(), v.end(),mem_fun_ref (&Person::showPerson));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::plusAge));
for_each(v.begin(), v.end(), mem_fun_ref(&Person::showPerson));
}