仿函数:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//仿函数(函数对象)重载“()”操作符 使类对象可以像函数那样调用
//仿函数是一个类,不是一个函数
//函数对象可以像普通函数一样调用
//函数对象可以像普通函数那样接收参数
//函数对象超出了函数的概念,函数对象可以保存函数调用的状态struct MyPrint { void operator()(int val) {cout << val;}
};
void test01() {MyPrint print;print(10);
}
#if 0
int num = 0; //在开发中,避免使用全局变量 多个线程对变量处理 加锁解锁
void MyPrint02(int val) {cout << val;num++;
}
#endif
//避免全局变量的方法
struct MyPrint02 {MyPrint02(){mNum = 0;}void operator()(int val) {mNum++;cout << val<<endl;}
public:int mNum;
};void test02() {MyPrint02 print;print(10);print(20);cout << print.mNum << endl;
}
void test03() {vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);MyPrint02 print;MyPrint02 print2=for_each(v.begin(), v.end(), print);cout << "print调用次数:" << print.mNum << endl; //0cout << "print2调用次数:" <<print2.mNum << endl; //4}
#if 1
int main() {cout << "test01" << endl;test01();cout << endl<<"test02" << endl;test02();cout <<endl<< "test03" << endl;test03();return 0;
}
#endif
#include<iostream>
#include<functional>
using namespace std;void test01() {//使用内建函数对象声明一个对象plus<int> myclass;cout<<myclass(10, 20)<<endl;//使用匿名临时对象cout << plus<int>()(5, 6) << endl;
}
#if 1
int main() {test01() ;return 0;
}
#endif
#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
//仿函数适配器 bindlst bind2nd 绑定适配器
struct MyPrint {void operator()(int v) {cout << v << " ";}
};
struct MyPrint2 :public binary_function<int,int,void>{ //第一个参数类型 第二个参数类型 返回值类型void operator()(int v,int value) const{cout << "v:" << v <<" " <<"val:" << value << endl;//cout << v+value << " ";}
};
void test01() {vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}MyPrint print;for_each(v.begin(), v.end(), print);cout << endl;for_each(v.begin(), v.end(), MyPrint());cout << endl;//如何给仿函数传入两个参数//for_each(v.begin(), v.end(), MyPrint2(100)); 错误int addNum = 100;cout << "------------bind2nd-----------------" << endl;for_each(v.begin(), v.end(), bind2nd(MyPrint2(),addNum));cout << "------------bind1st-----------------" << endl;for_each(v.begin(), v.end(), bind1st(MyPrint2(), addNum));//绑定适配器 将一个二元函数对象转换为一元函数对象//bindlst bind2nd 区别//bindlst将addNum绑定为函数对象的第一个参数//bind2nd将addNum绑定为函数对象的第二个参数
}
struct MyCompare01 {bool operator()(int v1, int v2){return v1 > v2;}
};
struct MyCompare02 :public binary_function<int,int,bool>{bool operator()(int v1, int v2) const{return v1 > v2;}
};
struct MyGreater5 {bool operator()(int v) {return v > 5;}
};
struct MyGreater55:public unary_function<int,bool> {bool operator()(int v) const {return v > 5;}
};
struct MyPrint02 {void operator()(int v) {cout << v << " ";}
};
//仿函数适配器 not1 not2 取反适配器
void test02() {vector<int> v;for (int i = 0; i < 10; i++) {/*v.push_back(rand()%100+10);*/v.push_back(i);}for_each(v.begin(), v.end(), MyPrint02());cout <<endl<< "-------排序后-------"<<endl;sort(v.begin(), v.end(), MyCompare01());for_each(v.begin(), v.end(), MyPrint02()); cout<<endl << "-------取反排序后-------" << endl;sort(v.begin(), v.end(), not2(MyCompare02()));for_each(v.begin(), v.end(), MyPrint02());//not1 和 not2 区别//如果对二元谓词取反 用 not2//如果对一元谓词取反 用 not1cout <<endl<< "not1" << endl;vector<int>::iterator ret=find_if(v.begin(), v.end(), MyGreater5());cout << *ret << endl;vector<int>::iterator ret1 = find_if(v.begin(), v.end(),not1( MyGreater55()));if (ret1 == v.end()) {cout << "没有找到" << endl;}elsecout << *ret1 << endl;}
void MyPrint033(int val1,int val2) {cout << val1 << " "<<val2<<" "<<endl;
}
void MyPrint03(int val) {cout << val << " ";
}
//仿函数适配器 ptr_fun 把普通函数 转成 函数对象
void test03() {vector<int> v;for (int i = 0; i < 10; i++) {/*v.push_back(rand()%100+10);*/v.push_back(i);}for_each(v.begin(), v.end(), MyPrint03); //此处函数没有参数cout << endl;//把普通函数 转成 函数对象for_each(v.begin(), v.end(), bind2nd(ptr_fun(MyPrint033),10)); //此处函数没有参数
}
//仿函数适配器 mem_fun mem_fun_ref
class Person {
public:/*Person(int age, int id) {错误age = age;id = id;}*/ /*Person(int age, int id) { 正确this->age = age;this->id = id;}*/Person(int age, int id) :age(age), id(id) {} //正确void show() {cout << "age:" << age << "id:" << id << endl;}
public:int age;int id;
};
struct PrintPerson {void operator()(Person p) {p.show();}
};
void test04() {//如果容器中存放的是对象或者对象指针,我们for_each打印时,调用类//自己提供的打印函数vector<Person> vp;Person p1(10, 20);Person p2(20, 30);Person p3(30, 40);Person p4(40, 50);vp.push_back(p1);vp.push_back(p2);vp.push_back(p3);vp.push_back(p4);for_each(vp.begin(), vp.end(), PrintPerson());cout << "-----mem_fun_ref使用--------" << endl;for_each(vp.begin(), vp.end(), mem_fun_ref(&Person::show));vector<Person*> vpp;vpp.push_back(&p1);vpp.push_back(&p2);vpp.push_back(&p3);vpp.push_back(&p4);cout << "-----mem_fun使用--------" << endl;for_each(vpp.begin(), vpp.end(), mem_fun(&Person::show));//mem_fun_ref mem_fun区别?//如果存放的是对象指针用mem_fun//如果存放的是对象,使用mem_fun_ref}
#if 1
int main() {cout << "test01" << endl;test01();cout << endl<<"test02" << endl;test02();cout <<endl<< "test03" << endl;test03();cout << endl << "test04" << endl;test04();
}
#endif