#include <iterator>
#include <iostream>
template<class _InIt,
class _Pr > inline _InIt myfind_if(_InIt _First,_InIt _Last,_Pr _Pred)
{
for (; _First != _Last; _First++){
if (_Pred(*_First)){
break;
}
}
return (_First);
}
int _tmain(int argc, _TCHAR* argv[])
{
int values[] = { 100, 200, 300, 400, 500 };
auto p = myfind_if(values, end(values), [](int x){ return x >= 400; });
cout << *p << endl;
system("pause");
return 0;
}
使用函数模版最大的变化在于普通的函数是基于参数的类型来判断参数的合法性,而函数模版是基于参数的功能,具体就是如果是迭代器,无论是整型迭代器还是浮点迭代器都行,而谓词就是只要实现了operator()成员函数的都行,当然还要看是一元还是二元。有点类似python的鸭子类型。