Create objects that can be called like functions, for use with STL algorithms.
A functor, or function object, is an instance of a C++ class that has its function call operator, `operator()`, overloaded. Because it overloads this operator, an object of the class can be called as if it were a regular function. For example, if you have `MyFunctor functorObject;`, you can invoke it with `functorObject(arg1, arg2);`. The main advantage of functors over regular functions is that they can store state. Since a functor is an object, it can have member variables. This allows the functor to maintain information across multiple calls. For instance, a functor used with `std::for_each` could count how many elements in a range meet a certain condition, storing the count in a member variable. Before C++11 and lambda expressions, functors were the primary way to pass custom, stateful operations to STL algorithms. For example, to sort a container in a custom order, you could pass an instance of a functor to `std::sort`. The functor's `operator()` would take two arguments of the container's element type and return `true` if the first argument should come before the second in the sorted order. While lambda expressions are often more convenient for simple cases today, understanding functors is still important as they are used throughout the STL and other C++ libraries, and they offer more flexibility for complex, stateful operations.