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
|
#include <functional>
#include <iostream>
namespace
{
template <typename InputIterator, typename Size, typename UnaryPredicate>
Size find_if( InputIterator first, Size n, UnaryPredicate unary_predicate )
{
const Size npos( -1 );
if ( n == 0 ) return ( npos );
if ( unary_predicate( *first ) ) return ( Size( 0 ) );
Size i = find_if( ++first, --n, unary_predicate );
return ( ( i == npos ) ? npos : ++i );
}
}
int main()
{
int a[] = { 1, 2, 3 };
int b[] = { 1, 2, -3 };
int c[] = { 1, -2, 3 };
int d[] = { -1, 2, 3 };
std::cout << "a[] - " << find_if( a, 3, std::bind2nd( std::less<int>(), 0 ) ) << std::endl;
std::cout << "b[] - " << find_if( b, 3, std::bind2nd( std::less<int>(), 0 ) ) << std::endl;
std::cout << "c[] - " << find_if( c, 3, std::bind2nd( std::less<int>(), 0 ) ) << std::endl;
std::cout << "d[] - " << find_if( d, 3, std::bind2nd( std::less<int>(), 0 ) ) << std::endl;
return 0;
}
|