What is a pseudo-function? Simply put, an operator () function is defined in a class. This function is used in the algorithm function and uses this class as a parameter of the algorithm:
What is the difference between this and ordinary functions?
A function is also called a function object, such:
class FunctionObjectType{ public: void operator()(){ statements }}
This definition has three advantages:
1. the imitation function is more clever than the general function because it can always be in the state. In fact, for the imitation function, you can colleagues have two entities in different states, but the general function cannot catch it.
2. Each function has its type. Therefore, you can pass the type of the function as a template parameter to specify a behavior mode. There is also a benefit: the container type varies depending on the function simulation.
3. The execution speed is generally faster than that of normal functions.
Let's take a look at two equivalent examples:
#include<iostream>#include<list>#include<algorithm>#include<set>#include<string>#include<vector>using namespace std;class Person{public:Person(){}Person(string name,int age){this->name=name;this->age=age;}void show(){cout<<name<<" "<<age<<endl;}bool operator()(Person &p1,Person &p2){return p1.name<p2.name||(!(p1.name>p2.name))&&(p1.age<p2.age);}private:string name;int age;};int main(){vector<Person>v;Person p1("zhang",22);Person p2("li",20);Person p3("tan",23);Person p4("lai",22);Person p5("huang",22);Person p6("huang",21);Person p7("zhang",23);Person p8("zhang",17);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);v.push_back(p6);v.push_back(p7);v.push_back(p8);for(vector<Person>::iterator itera=v.begin();itera!=v.end();++itera){(*itera).show();}cout<<"*****************************"<<endl;sort(v.begin(),v.end(),Person());for(vector<Person>::iterator itera=v.begin();itera!=v.end();++itera){(*itera).show();}system("pause");return 0;}
#include<iostream>#include<list>#include<algorithm>#include<set>#include<string>#include<vector>using namespace std;class Person{public:Person(){}Person(string name,int age){this->name=name;this->age=age;}void show(){cout<<name<<" "<<age<<endl;}public:string name;int age;};bool bijiao(Person &p1,Person &p2){return p1.name<p2.name||(!(p1.name>p2.name))&&(p1.age<p2.age);}int main(){vector<Person>v;Person p1("zhang",22);Person p2("li",20);Person p3("tan",23);Person p4("lai",22);Person p5("huang",22);Person p6("huang",21);Person p7("zhang",23);Person p8("zhang",17);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);v.push_back(p6);v.push_back(p7);v.push_back(p8);for(vector<Person>::iterator itera=v.begin();itera!=v.end();++itera){(*itera).show();}cout<<"*****************************"<<endl;sort(v.begin(),v.end(),bijiao);for(vector<Person>::iterator itera=v.begin();itera!=v.end();++itera){(*itera).show();}system("pause");return 0;}
#include<iostream>#include<list>#include<algorithm>#include<set>#include<string>#include<vector>using namespace std;class Person{public:Person(){}Person(string name,int age){this->name=name;this->age=age;}void show(){cout<<name<<" "<<age<<endl;}public:string name;int age;};class BiJiao{public:bool operator()(const Person &p1,const Person &p2){return p1.name<p2.name||(!(p1.name>p2.name))&&(p1.age<p2.age);}};int main(){set<Person,BiJiao>v;Person p1("zhang",22);Person p2("li",20);Person p3("tan",23);Person p4("lai",22);Person p5("huang",22);Person p6("huang",21);Person p7("zhang",23);Person p8("zhang",17);v.insert(p1);v.insert(p2);v.insert(p3);v.insert(p4);v.insert(p5);v.insert(p6);v.insert(p7);v.insert(p8);for(set<Person,BiJiao>::iterator itera=v.begin();itera!=v.end();++itera){(*itera).show();}cout<<"*****************************"<<endl;for(set<Person,BiJiao>::iterator itera=v.begin();itera!=v.end();++itera){(*itera).show();}system("pause");return 0;}
The three results are the same: