Listing 2
#include <iostream>
using std::cout;
struct is_five
{ // silly function object
bool operator()(int i) const
{ // return true if i is 5
return i == 5;
}
};
template <class InIt, class Pr>
InIt find_if(InIt first, InIt last, Pr pred)
{ // return iterator to first element for
// which pred returns true
while (first != last && !pred(*first))
++first;
return first;
}
int main()
{ // show use of function object
int values[] = { 1, 1, 2, 3, 5, 8, 11, 19 };
int *res = find_if(
values, values + sizeof(values) / sizeof(*values), is_five());
cout << "offset is " << (res - values)
<< ", value is " << *res << '\n';
return 0;
}