The functors are the function objects in C++. The functor allows an instance object of some class to be called as if it were an ordinary function. Let us consider a function that takes one argument. We can use this function as function object to do some task on a set of data.
Example Code
#include <iostream>
#include <algorithm>
using namespace std;
int square(int x) {
return x*x; //return square of x
}
int main() {
int data[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
transform(data, data+10, data, square);
for (int i = 0; i<10; i++)
cout << data[i] << endl;
}Output
0 1 4 9 16 25 36 49 64 81