#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
发表回复