public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "learning c++" <learning_c@hotmail.com>
To: gcc-help@gcc.gnu.org
Subject: function object, function pointer and function.
Date: Mon, 30 Aug 2004 17:25:00 -0000	[thread overview]
Message-ID: <BAY12-F300yki4jsGvs00011487@hotmail.com> (raw)

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

             reply	other threads:[~2004-08-30 17:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-30 17:25 learning c++ [this message]
2004-08-30 18:22 ` Eljay Love-Jensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BAY12-F300yki4jsGvs00011487@hotmail.com \
    --to=learning_c@hotmail.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).