Using Function Objects (Functors) in STL / C++
Generically, function objects (or functors) are class instances whose member function operator() has been defined. This member function allows the object to be used with the same syntax as a regular function call, and therefore its type can be used as template parameter when a generic function type is expected. So why not just use straight function calls? 1. Function objects (functors) can contain states 2. A function object is a type, so it can be used as a template parameter. Unary Function Objects (single argument) to read a std::vector of integers and output them as a string separated by whitespaces Suppose you wanted to find the sum total of all the values contained in a vector. You could loop through them sequentially in a for loop and increment them, or you could use a functor. A suitable functor might look like this: [code language="cpp"] struct adder : public unary_function<double, void> { adder() : sum(0) {} double sum; void oper...