find find_if for_each

#include<iostream>
#include<list>
#include <algorithm>
#include <vector>
#include <memory>
using namespace std;

class Cperson
{
    public:
        Cperson(int ag){age=ag;}
        ~Cperson(void){age=0;
        }
        
        /*bool isreal(Cperson callback){
            return this->age==callback.age;
        } */
        bool operator() (Cperson callback) 
        {
            cout << "callback.age "<<callback.age << endl;
            return this->age==callback.age;
        } 
    public:
        int age; // 年龄
};

int main()
{
    Cperson* person1 =new Cperson(7);
    Cperson* person2 =new Cperson(2);
    Cperson* person3 =new Cperson(3);

    //Cperson stu1(1);
    list<Cperson*> lst;
    lst.push_back(person1);
    lst.push_back(person2);
    lst.push_back(person3);
    auto it = find(lst.begin(),lst.end(),person1);//find返回的是指针的指针
    Cperson it1 = **it;
    if (it != lst.end()) {
        //cout << it1.age << endl;
    }
    
    vector<int> lstint;
    lstint.push_back(8);
    lstint.push_back(9);
    lstint.push_back(10);
    auto it2 = find(lstint.begin(),lstint.end(),8);
    if (it2 != lstint.end()) {
        //cout << *it2 << endl;  //find返回的是对象的指针
    }
    
    Cperson person4(11),person5(12),person6(13);
    list<Cperson> lstperson;
    lstperson.push_back(person4);
    lstperson.push_back(person5);
    lstperson.push_back(person6);

    //auto it = find(lstperson.begin(),lstperson.end(),person4); //报错,对象无法==比较
    
    auto it3 = find_if(lstperson.begin(),lstperson.end(),[person5](Cperson callback)->bool {return person5.age==callback.age; });
    Cperson it33 = *it3;
    if (it3 != lstperson.end()) {
        cout << "lamdba "<<it33.age << endl;
    }
    
    auto it4 = find_if(lstperson.begin(),lstperson.end(),person5);
    Cperson it44 = *it4;
    if (it4!= lstperson.end()) {
        cout << "operator "<<it44.age << endl;
    }
    
    auto it5 = find_if(lstperson.begin(),lstperson.end(),Cperson(person6));
    Cperson it55 = *it5;
    if (it4!= lstperson.end()) {
        cout << "fun "<<it55.age << endl;
    }
    for_each(lstperson.begin(),lstperson.end(),person6);
    for_each(lstperson.begin(),lstperson.end(),[](Cperson callback)->bool {return callback.age; });
    delete person1;
    delete person2;
    delete person3;
    //*it =20;
    //cout << it1.age << endl;
   return 0;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值