大挪耗

c++find_if算法

#include "iostream"
#include "vector"
#include "list"
#include "algorithm"

using namespace::std;

template <typename T>
bool zsshow(const T&no)
{
    if (no!=7) return false;
    cout<<"亲爱的"<<no<<"号: 我是一只傻傻鸟。\n";
    return true;
}

template<typename T>
class czs//张三的个性话表白函数。
{
public:
    T m_no;//存放张三喜欢的超女编号。
    czs(const T&no):m_no(no){}//构造函数的参数是张三喜欢的超女编号。
    bool operator()(const T&no)
    {
        if (no!=m_no) return false;
        cout<<"亲爱的"<<no<<"号: 我是一只傻傻鸟。\n";
        return true;
    }
};

template<typename T1,typename T2>
T1 findif(const T1 first,const T1 last,T2 pfun)
{
    for (auto it=first;  it!=last ; it++) {
        if(pfun(*it)== true)return it; //用迭代器调用函数对象。
    }
    return last;
}

int main(){
    vector<int>bh={5,8,2,6,9,3,1,7};//存放超女编号的容器。

    auto it1= findif(bh.begin(),bh.end(), zsshow<int>);
    if (it1==bh.end())cout<<"查找失败。\n";
    else cout<<"查找成功:"<<*it1<<endl;


    auto it2= findif(bh.begin(),bh.end(), czs<int>(8));
    if (it2==bh.end())cout<<"查找失败。\n";
    else cout<<"查找成功:"<<*it2<<endl;
    //调用了#include "algorithm"的函数
    auto it3= find_if(bh.begin(),bh.end(), czs<int>(8));
    if (it3==bh.end())cout<<"查找失败。\n";
    else cout<<"查找成功:"<<*it2<<endl;
}

亲爱的7号: 我是一只傻傻鸟。
查找成功:7
亲爱的8号: 我是一只傻傻鸟。
查找成功:8
亲爱的8号: 我是一只傻傻鸟。
查找成功:8


已发布

分类

可以收藏大挪耗一下。下载麻烦点城通网盘,站长保证下载速度,不会限速的,放心点就是了;分卷,安卓下载为txt:程序下载为url,不会下载参考不会下载。如果你想让本站活的久一点,请直接捐助

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注