1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
template<class ForwardIterator1, class ForwardIterator2, class Predicate>
ForwardIterator1 search_end_if(ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2, Predicate Functor){
ForwardIterator1 ret = last1;
for (; first1 != last1; ++first1) {
ForwardIterator1 it1 = first1;
ForwardIterator2 it2 = first2;
while (*it1 == *it2) {
++it1;
++it2;
if (Functor(*last2)) {
ret = first1;
break;
}
if (Functor(*last1)) return ret;
}
}
return ret;
}
class Odd {
public:
bool operator()(int i) {
return i > 0;
}
};
int main() {
int* n;
int x[3] = { 41, 1, 2 };
int y[2] = { 1, 2 };
search_end_if(x, x + 3, y, y + 2, Odd());
Odd f;
n = search_end_if(x, x + 3, y, y + 2, f);
if (n == x + 3) std::cout << "\nNot found" << std::endl;
else std::cout << "\nFound on position number " << (n - x) << std::endl;
return 0;
}
|