* function object, function pointer and function.
@ 2004-08-30 17:25 learning c++
2004-08-30 18:22 ` Eljay Love-Jensen
0 siblings, 1 reply; 2+ messages in thread
From: learning c++ @ 2004-08-30 17:25 UTC (permalink / raw)
To: gcc-help
Hi, Everyone:
I compiled a short code that invovles function object and function pointer.
in the line: int count1 = count_if(v1.begin(), v1.end(),
&lessThan20Function);
I used the funtion poiter to pass the address of function. It works very
well,
However, when I deleted the symbol "&", it still works. "lessThan20Function"
is the name of function. Can we use the function name as an argument?
What is the advantage of function object?
Thanks in advanace!
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool lessThan20Function(int value)
{
return value < 20;
}
class LessThan20 {
public:
bool operator() (int value) const
{
return value < 20;
}
};
int main() {
int iarr[10] = {2,4,55,6,66,10,22,14,70,1234};
vector<int> v1(iarr, iarr+10);
LessThan20 lt20;
// count_if(start_iterator, end_iterator, predicate);
// Pointer to a function as argument
int count1 = count_if(v1.begin(), v1.end(), &lessThan20Function);
cout << "There are " << count1 << " values less than 20" << endl;
// Function object as argument
int count2 = count_if(v1.begin(), v1.end(), lt20);
cout << "There are " << count2 << " values less than 20" << endl;
return 0;
]
_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: function object, function pointer and function.
2004-08-30 17:25 function object, function pointer and function learning c++
@ 2004-08-30 18:22 ` Eljay Love-Jensen
0 siblings, 0 replies; 2+ messages in thread
From: Eljay Love-Jensen @ 2004-08-30 18:22 UTC (permalink / raw)
To: learning c++, gcc-help
Hi,
In the situation you presented, the & in your "int count1 =
count_if(v1.begin(), v1.end(), &lessThan20Function);" line is
superfluous. Although it is perfectly good code, and acceptable to all
compilers.
In my experience, it would be omitted. By convention.
Also in my experience, there is a preference to function objects over
function pointers.
For some really neat stuff, the BOOST (www.boost.org) library has a lambda
library, which would allow you to put the body of the predicate right in
your count_if statement -- and in a quite abbreviated form. It'd look
something like...
int count = count_if(v1.begin(), v1.end(), _1 > 20);
HTH,
--Eljay
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2004-08-30 17:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-30 17:25 function object, function pointer and function learning c++
2004-08-30 18:22 ` Eljay Love-Jensen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).