函数对象适配器
函数对象适配器是完成一些配接工作,这些配接包括绑定(bind)、否定(negate),以及对一般函数或成员函数的修饰,使其成为函数对象。
bind1st:将参数绑定为函数对象的第一个参数
bind2nd:将参数绑定为函数对象的第二个参数
not1:对一元函数对象取反
not2:对二元函数对象取反
函数对象适配器:
ptr_fun:将普通函数修饰成函数对象
成员函数适配器:
mem_fun:修饰成员函数
mem_fun_ref:修饰成员函数
1.绑定适配器:将一个二元函数对象转变为一元函数对象
bind1st
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct MyPrint:public binary_function<int,int,void>
//binary_function,int、int为operator参数,void为返回值
{
void operator()(int v,int val) const{//const防止修改本值
cout << "v=" << v << ",val=" << val ;
cout <<",v+val=" << v + val << " " << endl;
}
};
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
for_each(v.begin(), v.end(), bind1st(MyPrint(),100));//bind1st
}
bind2nd
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct MyPrint:public binary_function<int,int,void>
{
void operator()(int v,int val) const{
cout << "v=" << v << ",val=" << val ;
cout <<",v+val=" << v + val << " " << endl;
}
};
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd(MyPrint(),100));//bind2nd
}
2.取反适配器:not
struct Mycompare:public binary_function<int,int,bool>//binary_function
{
bool operator()(int v1, int v2) const{
return v1 > v2;
}
};
struct MyPrint01
{
void operator()(int v) {
cout << v <<" ";
}
};
void test02() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(rand()%100);
}
for_each(v.begin(), v.end(), MyPrint01());
cout << endl;
sort(v.begin(), v.end(), Mycompare());
for_each(v.begin(), v.end(), MyPrint01());
cout << endl;
sort(v.begin(), v.end(), not2(Mycompare()));//not2
for_each(v.begin(), v.end(), MyPrint01());
}
3.unary_function与binary_function区别、find_if()使用
struct MyPrint01
{
void operator()(int v) {
cout << v <<" ";
}
};
struct MyGreater5:public unary_function<int,bool>//unary_function,接受一元参数
{
bool operator()(int val) const{
return val > 5;//查找大于5的迭代器
}
};
void test02() {
vector<int> v;
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
for_each(v.begin(), v.end(), MyPrint01());
cout << endl;
vector<int>::iterator it= find_if(v.begin(), v.end(), MyGreater5());//find_if查找,返回迭代器
if (it == v.end()) {
cout << "不存在!" << endl;
}
else {
cout << *it << endl;
}
}
4.ptr_fun 将普通函数 转为 函数对象
普通函数:
void Myprint(int val) {
cout << val << " ";
}
void test01() {
vector v;
for (int i = 0; i < 10; i++)
v.push_back(i);
for_each(v.begin(), v.end(), Myprint);
}
使用ptr_fun,转为函数对象:
void Myprint(int val,int n) {
cout << val+n << " ";
}
void test01() {
vector<int> v;
for (int i = 0; i < 10; i++)
v.push_back(i);
for_each(v.begin(), v.end(), bind2nd(ptr_fun(Myprint),10));
}
5.mem_fun_ref
如果容器中存放的对象或对象指针,我们用for_each算法打印的时候,调用类自己提供的打印函数。
class Person {
public:
Person(int id, int age):id(id),age(age) {}
void Show() {
cout << "age:" << this->age << ",id:" << this->id << endl;
}
int id;
int age;
};
void test02() {
vector<Person> v;
Person p1(1, 2), p2(3, 4), p3(5, 6);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
//调用Person成员函数
for_each(v.begin(), v.end(), mem_fun_ref(&Person::Show));
}
6.mem_fun
class Person {
public:
Person(int id, int age):id(id),age(age) {}
void Show() {
cout << "age:" << this->age << ",id:" << this->id << endl;
}
int id;
int age;
};
void test03() {
vector<Person*> v;
Person p1(1, 2), p2(3, 4), p3(5, 6);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
for_each(v.begin(), v.end(), mem_fun(&Person::Show));
}
7.mem_fun和mem_fun_ref区别
如果存放对象指针,使用mem_fun;
如果存放对象,使用mem_fun_ref;