Reputation: 85
#include <iostream>
#include <algorithm>
using namespace std;
bool myfn(int i, int j) { return i<j; }
int main () {
int myints[] = {3,7,2,5,6,4,9};
// using function myfn as comp:
cout << "The smallest element is " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest element is " << *max_element(myints,myints+7,myfn) << endl;
return 0;
}
considering the above code , is there any difference if we passmyfn
or &myfn
to min_element
? when we tried to pass a functor which one would be a more standard way?
Upvotes: 3
Views: 313
Reputation: 83537
Typically, pass-by-reference and pass-by-value only refer to passing variables, not functions. (Template functions introduce some complications, but that is for another discussion.) Here you are passing myfn
as a pointer to a function. There is no difference if you use &myfn
instead. The compiler will implicitly convert myfn
to a pointer to the function with that name.
Upvotes: 10
Reputation: 361522
is there any difference if we pass myfn or &myfn to min_element?
No. myfn
gets implicitly converted into pointer-to-function, anyway. So there is no difference at all.
Upvotes: 6