here is a function
bool IsOdd (int i) { return ((i%2)==1); }
and they used it in the count_if but with no arguments...
mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
its Grt Confusion......
>here is a function
>bool IsOdd (int i) { return ((i%2)==1); }
>and they used it in the count_if but with no arguments...
>mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
This line doesn't call IsOdd. IsOdd will be called later by the count_if code, and when it is, it will be called with the appropriate parameter.
You have to pass an argument to the function when you call it.
The 'count_if' invocation does not call the function directly.
It passes the function itself as an argument.
Cheers & hth.,
- Alf
it means Automatic argument passing by the count_if function
it will not work is i use it as IsOdd
?
Maybe this will clear things out:
template<typename Function>
void callThisFunction(Function f)
{
f(1);
}
void someFunction(int i)
{
std::cout << i << std::endl;
}
int main()
{
callThisFunction(someFunction);
}
(The interesting (and a bit complicated) part is what 'Function' in
that template resolves to. In this particular case you don't need to
worry about that, though.)