Functor in C++In C++, a functor is a function object  an instance of a class that overloads the function call operator (). Functors are used to provide an objectoriented way of working with functions or functionlike objects. Functors can be used in many situations where a function is needed, including algorithms like std::transform() and std::sort() and in custom data structures like maps and sets. Here's an example of a functor that multiplies its argument by a fixed value: In this example, Multiplier is a class with a single member variable factor_, which is set in the constructor. The operator() function is overloaded to take an integer argument x and return x * factor_. We can use this functor like this: In this example, we create an instance of Multiplier with a factor_ value of 5. We then call the functor with an argument of 10, which returns 10 * 5 = 50. Note that we're able to call multiply_by_5() as if it were a regular function, but it's an object of type Multiplier. This is because the operator() function is overloaded, allowing us to call the object as if it were a function. Functors are often used in conjunction with templates to create flexible, reusable code that can work with different types of data. Need of Functors over Normal FunctionsFunctors have a number of advantages over normal functions in C++. Here are a few reasons why you might choose to use a functor instead of a normal function:
In summary, functors provide a flexible and powerful way to work with functions or functionlike objects in C++. They can be customized, encapsulated, polymorphic, stateful, and efficient. As a result, they are a popular and widelyused feature of the language. Types of C++ FunctorsThere are three types of functors available in c++. 1. Generator Functor:In C++, a generator functor is a type of functor that generates a sequence of values when called repeatedly. A functor is simply an object that behaves like a function in that it can be called with arguments and can return a value. A generator functor is typically implemented as a class that overloads the function call operator () and maintains the state between calls to generate a sequence of values. Here's an example: Example: Output: Explanation: This program creates a FibonacciGenerator object fib and then uses a for loop to generate the first 20 Fibonacci numbers by repeatedly calling the operator() function of the generator functor. The resulting sequence of numbers is printed to the console. 2. Unary Functor:In C++, a unary functor is a type of functor that takes a single argument of a certain type and returns a value of another type. A functor is simply an object that behaves like a function in that it can be called with arguments and can return a value. A unary functor is typically implemented as a class that overloads the function call operator () and takes a single argument of a specific type. Unary functors are useful for a wide variety of applications, such as transforming data or applying functions to sequences of values. They can be used to encapsulate a specific operation or transformation into a reusable object, making the code more modular and easier to maintain. Here's a complete program that demonstrates how to use the Square unary functor to compute the squares of a sequence of integers: Example: Output: Explanation: This program creates a Square unary functor object square and uses a for loop to call the operator() function of the functor with a sequence of integers. The resulting sequence of squares is printed to the console. Note that in this program, we have marked the operator() function of the Square functor as const to indicate that it does not modify the state of the functor. This is a good practice when defining functors, as it makes it clear that the functor is stateless and does not have any side effects. 3. Binary Functor:In C++, a binary functor is a type of functor that takes two arguments of specific types and returns a value of another type. A functor is simply an object that behaves like a function in that it can be called with arguments and can return a value. A binary functor is typically implemented as a class that overloads the function call operator () and takes two arguments of specific types. This binary functor takes two arguments, x and y, both of type int, and returns their sum. The operator() function is marked as const to indicate that it does not modify the state of the functor. Here's a complete program that demonstrates how to use the Add binary functor to compute the sum of two integers: Example: Output: Explanation: This program creates an Add binary functor object add and calls the operator() function of the functor with two integer arguments. The resulting sum is assigned to the result variable and printed to the console. Note that in this program, we have marked the operator() function of the Add functor as const to indicate that it does not modify the state of the functor. This is a good practice when defining functors, as it makes it clear that the functor is stateless and does not have any side effects. How to Create a C++ Functor?We can easily create functors in c++; for the purpose, we have to create a class first, then we have to overload the function call operator (), and after that, we can create an instance or an object of that specific class and can call that as a function. Let's understand this with the help of an example Example: Output:
Next TopicC++ GUI
