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 385413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:28:38+00:00 2026-05-12T15:28:38+00:00

I was wondering if anyone can help me with functors. I dont really understand

  • 0

I was wondering if anyone can help me with functors. I dont really understand what functors are and how they work I have tried googling it but i still dont get it. how do functors work and how do they work with templates

  • 1 1 Answer
  • 3 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-05-12T15:28:39+00:00Added an answer on May 12, 2026 at 3:28 pm

    A functor is basically a “function object”. It’s a single function which you have wrapped in a class or struct, and which you can pass to other functions.

    They work by creating your own class or struct which overloads the function call operator (called operator() ). Typically, you create an instance of it by simply constructing it in-place as an argument to your function which takes a functor.

    Suppose you have the following:

    std::vector<int> counts;
    

    Now, you want to increment all the counts that are contained in that vector. You could loop through them manually to increment them, or you could use a functor. A suitable functor, in this case, would look like this:

    struct IncrementFunctor
    {
        int operator() (int i)
        {
            return i + 1;
        }
    }
    

    IncrementFunctor is now a functor which takes any integer and increments it. To apply it to counts, you can use the std::transform function, which takes a functor as an argument.

    std::transform(
        counts.begin(),       // the start of the input range
        counts.end(),         // the end of the input range
        counts.begin(),       // the place where transform should place new values. 
                              // in this case, we put it right back into the original list.
        IncrementFunctor());  // an instance of your functor
    

    The syntax IncrementFunctor() creates an instance of that functor which is then passed directly to std::transform. You could, of course, create an instance as a local variable and pass it on, but this is much more convenient.

    Now, onto templates. The type of the functor in std::transform is a template argument. This is because std::transform does not know (or care!) of which type your functor is. All it cares about is that it has a fitting operator() defined, for which it can do something like

    newValue = functor(oldValue);
    

    The compiler is pretty smart about templates, and can often figure out on its own what the template arguments are. In this case, the compiler realizes automatically that you’re passing in a parameter of type IncrementFunctor, which is defined as a template type in std::transform. It does the same for the list, too, so the compiler automatically recognizes that the actual call would look like this:

    std::transform<std::vector<int>::iterator, // type of the input iterator
                   std::vector<int>::iterator, // type of the output iterator
                   IncrementFunctor>(          // type of your functor
        counts.begin(),       // the start of the input range
        counts.end(),         // the end of the input range
        counts.begin(),       // the place where transform should place new values. 
                              // in this case, we put it right back into the original list.
        IncrementFunctor());  // an instance of your functor
    

    It saves you quite a bit of typing. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

wondering if anyone can help. This works, but I was wondering how it would
Wondering if anyone can help with this. I have a table with some fixed
Wondering if anyone can help - I'm sure this was working before, but I
Wondering if anyone can help me with this annoying but trivial (in terms of
Wondering if anyone can help me understand how the localisation works with keywords. I
I'm wondering if anyone can help me. I have a wcf service running over
I was wondering if anyone can help me understand how exactly to create different
I was wondering if anyone can point me in the right direction. I have
I know I can get answers here but I was wondering if anyone knows
Just wondering if anyone can help me with this slight issue. I'm writing a

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.