函数对象(仿函数)适配器
文章目录
- 函数对象(仿函数)适配器
- 函数适配器
- 作用
- bind1st和bind2nd区别?
- 示例
- 取反适配器
- 一元取反适配器 not1
- 二元取反适配器 not2
- 函数指针适配器 ptr_fun
- 成员函数适配器 mem_fun_ref 和 mem_fun
函数对象(仿函数)适配器
函数对象绑定参数 ,将多个参数绑定为一个参数
函数适配器
作用
函数对象绑定参数 ,将多个参数绑定为一个参数
bind1st和bind2nd区别?
-
bind1st : 将参数绑定为函数对象的第一个参数
-
bind2nd : 将参数绑定为函数对象的第二个参数
-
bind1st bind2nd将二元函数对象转为一元函数对象
示例
#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;/*1、绑定 bind2nd2、继承 binary_function3、加const*/
class MyPrint :public binary_function<int,int,void> //这里继承binary_function<参数类型,参数类型,返回值类型>
{
public:void operator()(int val,int start)const //这里加const{cout << val + start << endl;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}cout << "请输入起始累加值:" << endl;int start = 0;cin >> start;for_each(v.begin(), v.end(), bind2nd(MyPrint(), start)); //bind2nd//for_each(v.begin(), v.end(), bind1st(MyPrint(), start));}
取反适配器
一元取反适配器 not1
#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;class GreaterThenFive : public unary_function<int, bool>
{
public:bool operator()(int val) const{return val > 5;}
};//取反适配器
void test()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}//一元取反//vector<int>::iterator itPos = find_if(v.begin(), v.end(), not1(GreaterThenFive()));//优化后的语法vector<int>::iterator itPos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 5)));if (itPos != v.end()){cout << "找到了元素:" << *itPos << endl;}
}
二元取反适配器 not2
#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;void test()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}sort(v.begin(), v.end(), not2(less<int>()));for_each(v.begin(), v.end(), [](int val) {cout << val << " ";});cout << endl;
}
函数指针适配器 ptr_fun
#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;//函数指针适配器
void myPrint(int val, int start)
{cout << val + start << endl;
}
void test()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}cout << "请输入起始累加值:" << endl;int start = 0;cin >> start;//这里我们提供的是回调函数,所以需要将函数指针适配成函数对象 ptr_fun(myPrint)for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint),start));
}
成员函数适配器 mem_fun_ref 和 mem_fun
- 如果容器中存放的是对象实体,那么用mem_fun_ref
- 如果容器存放的是对象指针, 那么用mem_fun
#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
#include<string>
using namespace std;class Person
{
public:Person(string name, int age) : m_name(name), m_age(age){}void ShowPerson(){cout << "name:" << m_name << " age:" << m_age << endl;}void AddAge(){m_age++;}string m_name;int m_age;
};//成员函数适配器
void test01()
{//如果容器中存放的是对象实体,那么用mem_fun_refvector<Person>v;v.reserve(3);Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);v.push_back(p1);v.push_back(p2);v.push_back(p3);//成员函数适配器 mem_fun_ref(成员函数的入口地址)for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));for_each(v.begin(), v.end(), mem_fun_ref(&Person::AddAge));for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));}void test02()
{//如果容器存放的是对象指针, 那么用mem_funvector<Person*>v;v.reserve(3);Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);//成员函数适配器 mem_fun(成员函数的入口地址)for_each(v.begin(), v.end(), mem_fun(&Person::ShowPerson));for_each(v.begin(), v.end(), mem_fun(&Person::AddAge));for_each(v.begin(), v.end(), mem_fun(&Person::ShowPerson));
}