Functors in C++
Functors are not a very popular thing among beginner or intermediate-level programmers. But this thing is very useful and helpful. The name functor suggests us some similarities with function. It is true that it surely has a connection with function. Often it is called a function pointer. Now, we have to understand the context of this functor. Suppose, in programming, you face a situation where you need to add the count of parameters in a function. But this task is restricted. You can not increase the parameters. So, now you can think of another function. Maybe you can get the idea of using a global variable instead of using parameters. So, these methods can do your job, but they are not optimised. Here come functors. By using functors, we can easily pass more parameters to a function. In this article, we will discuss functors in detail. Keep reading to know more about this thing.
What is a functor?
The term "functor" refers to an object that can be used to refer to a function or function pointer. The most frequent application of functors is in conjunction with STLs. Any object that may be used with () in the same way as a function is a functor. The () operator (function call operator) is overloaded for class objects that are regular functions, pointers to functions, and class objects for which the function operator()() is specified. When a regular function fails, we can occasionally utilise a function object. The STL offers a number of really useful function objects and frequently uses them. Another illustration of the efficacy of generic programming and the idea of pure abstraction is the concept of function objects. Anything that behaves like a function is a function, we could argue.
What is the scenario for using functors?
We have discussed one case at the beginning of the article. Functors are mainly used when we need to change the parameter, but it is restricted. Mainly in STL, it is used very much. Suppose you are writing one program where you have to use the sorting algorithm provided by the STL. Now it will only give you the output in ascending order. Suppose you want to implement this sorting with some extra conditions. Here you can use functor. See the below given example where we have used the transform operator to transform the array by using one function.
Example:
#include <bits/stdC++.h>
using namespace std;
int fun( int x ) {
x = x + 1;
return x;
}
int main()
{
int arr[] = { 12, 14, 2, 3, 8, 10, 7, 6, 20, 30, 15};
int n = sizeof(arr)/sizeof(arr[0]);
transform(arr, arr+n, arr, fun);
for (int i=0; i<n; i++)
cout << arr[i] <<" ";
return 0;
}
Output:

Code Explanation:
As you can see from the previous code, the program will increase our array by inserting value one into each value of the array. We have used the transform function from STL to do this. We have declared one function, named fun, to increment the value by 1. In the function, we have given only one parameter, which is the value from the indexed array. Now suppose one user is demanding that he wants to change the value of the user randomly. He wants to change the values of the array by incrementing it randomly. Now we have no choice left but to add one more parameter in the function. But wait, it is not that much easy because the transform function only takes the function with one parameter as input.
So, here we face the problem. We cannot provide a number to fun because transform requires a unary function (a function with only one argument) for an array (). And as a result, we would have to construct many functions to add each integer. Functors are useful in situations like these. A C++ class that simulates a function is known as a functor (or function object). The same function call syntax is used to call a functor. We develop an object that overloads the operator to produce a functor (). You can see the implementation below the given code.
Example:
#include <bits/stdC++.h>
using namespace std;
class fun
{
private:
int num;
public:
fun(int n) : num(n) { }
int operator () (int arr_num) const {
return num + arr_num;
}
};
int main()
{
int arr[] = { 12, 14, 2, 3, 8, 10, 7, 6, 20, 30, 15};
int n = sizeof(arr)/sizeof(arr[0]);
int add = 5;
transform( arr, arr+n, arr, fun( add ) );
for (int i=0; i<n; i++)
cout << arr[i] << " ";
}
Output:
