find is equivalent to find_if with operator== as the predicate.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13
vector<int> v;
// put stuff in it
find( v.begin(), v.end(), 5 );
//is equivalent to:
struct IsEqualTo5 {
booloperator()( int x ) const
{ return x == 5; }
};
find_if( v.begin(), v.end(), IsEqualTo5() );
Same goes for replace/replace_if and count/count_if
// simplified a bit here...
template< typename Iter, typename T >
Iter find( Iter first, Iter last, T val )
{
for( ; first != last; ++first )
if( *first == val )
return first;
return first;
}
template< typename Iter, typename Compare >
Iter find_if( Iter first, Iter last, Compare comp )
{
for( ; first != last; ++first )
if( comp( *first ) ) // This is the only difference
return first;
return first;
}
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
usingnamespace std;
// Here is our example string
string pi = "3.14159265358979323846264338327950288419716939937510";
// Here is a predicate function.
// It takes as argument a digit.
// If it is a numeric value evenly divisible by three, then it returns true.
// Otherwise, it returns false.
bool divisible_by_three( char digit )
{
return isdigit( digit ) && (digit != '0') && (((digit - '0') % 3) == 0);
}
int main()
{
cout << "The value of pi to " << (pi.length() - 2) << " decimal places is\n"
<< pi
<< "\n\n";
cout << "The number of digits equal to '4' is "
<< count( pi.begin(), pi.end(), '4' )
<< "\n\n";
cout << "The number of digits evenly divisible by 3 is "
<< count_if( pi.begin(), pi.end(), divisible_by_three )
<< "\n\n";
return 0;
}
A predicate may either be an actual function, as in the example, or a functor -- an object that behaves like a function. I could have coded it like this:
1 2 3 4 5 6 7 8 9 10 11
// Here is a predicate functor.
// It takes as argument a digit.
// If it is a numeric value evenly divisible by three, then it returns true.
// Otherwise, it returns false.
struct divisible_by_three
{
booloperator () ( constchar& digit ) const
{
return isdigit( digit ) && (digit != '0') && (((digit - '0') % 3) == 0);
}
};
1 2 3
cout << "The number of digits evenly divisible by 3 is "
<< count_if( pi.begin(), pi.end(), divisible_by_three() )
<< "\n\n";
Duoas, is here some mistake or there are really no brackets after "divisible_by_three" after the first count_if ?
1 2 3 4 5 6 7
cout << "The number of digits evenly divisible by 3 is "
<< count_if( pi.begin(), pi.end(), divisible_by_three )
<< "\n\n";
cout << "The number of digits evenly divisible by 3 is "
<< count_if( pi.begin(), pi.end(), divisible_by_three() )
<< "\n\n";
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
usingnamespace std;
// Here is our example string
string pi = "3.14159265358979323846264338327950288419716939937510";
// Here is a predicate functor.
// It takes as argument a digit.
// If it is a numeric value evenly divisible by three, then it returns true.
// Otherwise, it returns false.
struct divisible_by_three
{
booloperator () ( constchar& digit ) const
{
return isdigit( digit ) && (digit != '0') && (((digit - '0') % 3) == 0);
}
};
int main()
{
cout << "The value of pi to " << (pi.length() - 2) << " decimal places is\n"
<< pi
<< "\n\n";
// Here we create a named instance of the class (its name is "db3")
divisible_by_three db3;
cout << "The number of digits evenly divisible by 3 is "
<< count_if( pi.begin(), pi.end(), db3 )
<< "\n\n";
// Here we create a temporary instance of the class (it has no name)
cout << "The number of digits evenly divisible by 3 is "
<< count_if( pi.begin(), pi.end(), divisible_by_three() )
<< "\n\n";
return 0;
}