#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
//预定义函数对象:
//④成员函数适配器 mem_fun mem_fun_ref
class Person{
public:
Person(int age,int id):mAge(age),mId(id){
}
void show(){
cout<<"age = "<<mAge<<" id = "<<mId<<endl;
}
public:
int mAge;
int mId;
};
void test01(){
//如果容器中存放的是对象或者是对象指针
//我们for_each()算法打印的时候,调用自己的打印函数
vector<Person> v1;
Person p1(1,2),p2(3,4),p3(5,6);
v1.push_back(p1);
v1.push_back(p2);
v1.push_back(p3);
//打印(使用 类的成员函数)
for_each(v1.begin(),v1.end(),mem_fun_ref(&Person::show));
cout<<endl;
vector<Person*> v2;
v2.push_back(&p1);
v2.push_back(&p2);
v2.push_back(&p3);
//打印(使用 类的成员函数)
for_each(v2.begin(),v2.end(),mem_fun(&Person::show));
cout<<endl;
// mem_fun_ref 和 mem_fun 区别:
//如果存放的是对象 使用 mem_fun_ref
//如果存放的是对象指针 使用 mem_fun
}
int main(){
test01();
return 0;
}