0. outline

be careful : Function objects and functors refer to the same thing .
1.Function Object( Function object - functor ) Concept of
so-called function object( Or functor), It's a definition operator() An object of the class . FunctionObjectType fo;
... fo(...); 
!!!! be careful : expression fo() Is the calling function object fo of operator(), Instead of calling functions fo(). 

General writing method of function object :
class FunctionObjectType { public: void operator() () { statements } };
1.1.1 Advantages of imitative function over general function
Advantages of affine function : 1. A functor is an object , You can have member functions and member variables , That is, the affine function has a state ; 2. Each functor has its own type ;
3. Imitative functions are usually faster than general functions ( A lot of information is determined at compile time ).

1.1.2 Taking imitative function as sorting criterion  
#include <iostream> #include <string> #include <deque> #include <set>
#include <algorithm> using namespace std; /*Person class */ class Person { private:
string fn; // first name string ln; // last name public: Person() {}
Person(const string &f, const string &n) : fn(f), ln(n) {} string firstname()
const; string lastname() const; }; inline string Person::firstname() const {
return fn; } inline string Person::lastname() const { return ln; } ostream
&operator<<(ostream &s, const Person &p) { s << "[" << p.firstname() << " " <<
p.lastname() << "]"; return s; } /* class for function predicate * - operator
() returns whether a person is less than another person */ class
PersonSortCriterion { public: bool operator()(const Person &p1, const Person
&p2) const { /* a person is less than another person * - if the last name is
less * - if the last name is equal and the first name is less */ return
p1.lastname() < p2.lastname() || ( p1.lastname() == p2.lastname() &&
p1.firstname() < p2.firstname()); } }; int main() { Person p1("nicolai",
"josuttis"); Person p2("ulli", "josuttis"); Person p3("anica", "josuttis");
Person p4("lucas", "josuttis"); Person p5("lucas", "otto"); Person p6("lucas",
"arm"); Person p7("anica", "holle"); // declare set type with special sorting
criterion typedef set<Person, PersonSortCriterion> PersonSet; // create such a
collection PersonSet coll; coll.insert(p1); coll.insert(p2); coll.insert(p3);
coll.insert(p4); coll.insert(p5); coll.insert(p6); coll.insert(p7); // do
something with the elements // - in this case: output them cout << "set:" <<
endl; PersonSet::iterator pos; for (pos = coll.begin(); pos != coll.end();
++pos) { cout << *pos << endl; } } /* notes : there set coll Use special sorting criteria
PersonSortCriterion, And it is a function object class.
PersonSortCriterion So defined operator(): Compare their surnames first , If they are equal, compare their names . coll Constructors are generated automatically class
PersonSortCriterion An example of (instance), All elements are This will be used as the sorting criterion .
be careful , Sorting criteria PersonSortCriterion It's a class, So you can think of it as set of template Argument .
If ordinary functions are used as sorting criteria , You can't do that . " Take the above types as the sorting criteria " All set, Each has its own unique type . Can't put this set take
Come and " Have different sorting criteria " Other set Merge or assign . Although no matter what you do, you can't avoid it set Automatic sorting of nature , But you can design " Different ranking criteria have the same type " Imitative function of .
*/

Technology
Daily Recommendation