美文网首页
成员函数适配器

成员函数适配器

作者: python小青 | 来源:发表于2019-10-30 14:53 被阅读0次

#include

//成员函数适配器

#include

using namespace std;

class Person{

public:

    Person(string name,int age):m_name(name),m_age(age){}

    string m_name;

    int m_age;

    void showInfo(){

        cout<<"姓名:"<<m_name<<" 年龄:"<<m_age<<endl;

    }

    void agePlus(){

        this->m_age++;

    }

};

int main()

{

    vector v;

    Person p1("张三",19);

    Person p2("李四",20);

    Person p3("王五",21);

    v.push_back(p1);

    v.push_back(p2);

    v.push_back(p3);

    for_each(v.begin(),v.end(),mem_fun_ref(&Person::showInfo));//如果vector里面放的是Person指针,此处使用for_each(v.begin(),v.end(),mem_fun(&Person::showInfo))

    for_each(v.begin(),v.end(),mem_fun_ref(&Person::agePlus));

    cout<<"----------------------"<<endl;

    for_each(v.begin(),v.end(),mem_fun_ref(&Person::showInfo));

    return 0;

}

相关文章

网友评论

      本文标题:成员函数适配器

      本文链接:https://www.haomeiwen.com/subject/babovctx.html