Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:10:57+00:00 2026-06-01T08:10:57+00:00

I have a simple problem but I don’t know how to solve it because

  • 0

I have a simple problem but I don’t know how to solve it because I have never used functors in C++.

I want to do something like that (it is just an example) :

class MyClass
{
    void applyFunction(myFunction);  /* WRONG SYNTAX */
    double *_x;
    unsigned int *_size;
};

void MyClass::applyFunction(myFunction) /* WRONG SYNTAX */
{
    for (unsigned int i = 0; i < _size; ++i)
        myFunction(_x[i], 10.);
}

class OtherClass
{
    void myFunction1(double x, double lim);
    void myFunction2(double x, double lim);
    std::vector _v;
};

void OtherClass::myFunction1(double x, double lim)
{
    _v.clear();
    if (x > lim)
        _v.push_back(x);
}

void OtherClass::myFunction2(double x, double lim)
{
    _v.clear();
    if (x < lim)
        _v.push_back(x);
}

int main()
{
    MyClass myClass;
    OtherClass otherClass;
    myClass.applyFunction(otherClass.myFunction1); /* WRONG SYNTAX */
    std::cout<<otherClass._v.size()<<std::endl;
    myClass.applyFunction(otherClass.myFunction2); /* WRONG SYNTAX */
    std::cout<<otherClass._v.size()<<std::endl;
        return 0;
}

What would be the correct syntax to use functors/std::functions ?

Thank you very much !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:10:59+00:00Added an answer on June 1, 2026 at 8:10 am

    I’ll take you at your word that you want to use functors for this. Just for grins, I’ll also assume you want to do this the “right” way, not just find a syntax that will let it compile (and probably run, perhaps doing what you wanted).

    In this case, the standard library already has algorithms to support much of what you’re doing (especially in C++11). To copy the data that meets some criteria into a target vector, you have std::copy_if (though that’s missing in C++98/03 — you have to reverse the sense of the comparison and use std::remove_copy_if).

    Using this, your code becomes something like this:

    template <class T>
    class less_than {
        T limit;
    public:
        less_than(T lim) : limit(lim) {}
        bool operator()(T const &val) { return val < limit; }
    };
    
    std::copy_if(source.begin(), 
                 source.end(), 
                 std::back_inserter(target), 
                 less_than<int>(10));
    

    However, if you have C++11 available, it’s probably more convenient to use a lambda instead:

    std::copy_if(source.begin(), 
                 source.end(), 
                 std::inserter(target), 
                 [](int v) { return v < 10;});
    

    The lambda is basically just a way of getting the compiler to generate an anonymous functor class for you, so there’s not much real difference between the two, but the lambda obviously saves quite a bit of typing.

    If you’re stuck with C++03, you basically just invert the comparison:

    template <class T>  
    class greater_than { 
        T limit;
    public:
        bool operator()(T const &val) {
            return val > limit;
        }
    };
    
    std::remove_copy_if(src.begin(), 
                        src.end(), 
                        std::back_inserter(dst),
                        greater_than(10));
    

    Alternatively, you could write your own copy_if pretty easily — it was left out of C++98/03 mostly by oversight, not because it needs anything the language doesn’t provide, or anything like that (though as I recall, getting all the border conditions exactly right can be a little tricky).

    For what it’s worth, I should also note that the standard library does have std::less and std::greater, so the less_than and greater_than functors I’ve given above aren’t really necessary. Unfortunately, they just do the comparison, so to use them as we’re doing here, you have to use std::bind1st or std::bind2nd to get them to compare to a constant:

    std::remove_copy_if(src.begin(),
                        src.end(),
                        std::ostream_iterator<int>(std::cout, "\n"),
                        std::bind1st(std::less<int>(), 10));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have, I believe, a simple problem, but I don't know how to solve
I have a simple problem but I am not sure how to solve it.
I have a very simple problem, but I can't seem to solve it. I
It looks like a problem which could have simple solution, but I haven't found
This may seem like a simple problem to solve, but I'm new to Android
I have a simple problem but I just don't understand any of the examples
I have a really simple problem, but I don't find a nice way how
I have a simple problem but no matter what I try I can't see
Hi I am new to PHP and have a simple problem but I have
I have a very simple problem but cannot find a nice solution. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.