Related blog posts: Exploration of C++ New Features (9): functor
Functor object simulation function
Use class objects like function names.
A functor is to make the use of a class look like a function. Its implementation is to implement an operator() in the class, and this class has a function-like behavior, which is a functor class.
The syntax format of operator( ):
Example 1:
Attach example code:
//小问学编程
#include<iostream>
#include<vector>
using namespace std;
class Pow
{
public:
int operator()(int i)
{
return i*i;
}
double operator()(double d)
{
return d*d;
}
};
int main()
{
Pow pow;
int i=pow(4);
double d=pow(5.5);
cout<<i<<endl;
cout<<d<<endl;
return 0;
}
例2:pow->mypow
Attach example code:
//小问学编程
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class Pow
{
public:
double operator()(double x,int y)
{
int ret=1;
for(int i=0;i<y;i++)
ret*=x;
return ret;
}
};
int main()
{
int a=2;
cout<<pow(a,5)<<endl;
Pow mypow;
cout<<mypow(a,5)<<endl;
cout<<mypow.operator()(a,5)<<endl;
return 0;
}
Supplementary example:
Attached supplementary example code:
//小问学编程
#include<iostream>
using namespace std;
class Myclass
{
public:
Myclass(int x):_x(x){
};
int operator()(const int n)const{
return n*_x;
}
private:
int _x;
};
int main()
{
Myclass Obj1(3);
cout<<Obj1(6)<<endl;
return 0;
}
Example 3: std::sort callback function
The first type, the
second
type, and the third type
Example 4: The advantage of functors is that they are in the form of objects, which can carry more information for making judgments. For example, when the object is initialized, we can pass in parameters to determine the state without modifying the source code.
The program carries information, and the ascending and descending order is determined by me
Example 3 and 4 code:
第一种
//小问学编程
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
bool Compare(int a,int b)
{
return a>b;
}
int main()
{
int arr[7]={
3,5,1,5,2,6,8};
vector<int> vi(arr,arr+6);
//Compare c;
sort(vi.begin(),vi.end(),Compare);
for(auto itr=vi.begin();itr!=vi.end();++itr)
{
cout<<*itr<<endl;
}
return 0;
}
第二种
//小问学编程
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class Compare
{
public:
bool operator()(int a,int b)
{
return a>b;
}
};
int main()
{
int arr[7]={
3,5,1,5,2,6,8};
vector<int> vi(arr,arr+6);
Compare c;
sort(vi.begin(),vi.end(),c);//放一个对象
for(auto itr=vi.begin();itr!=vi.end();++itr)
{
cout<<*itr<<endl;
}
return 0;
}
第三种
//小问学编程
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class Compare
{
public:
bool operator()(int a,int b)
{
return a>b;
}
};
int main()
{
int arr[7]={
3,5,1,5,2,6,8};
vector<int> vi(arr,arr+6);
sort(vi.begin(),vi.end(),Compare());//放一个临时对象
for(auto itr=vi.begin();itr!=vi.end();++itr)
{
cout<<*itr<<endl;
}
return 0;
}
第四种 携带信息,升序降序由我决定
//小问学编程
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class Compare
{
public:
Compare(bool b):flag(b){
}
bool operator()(int a,int b)
{
if(flag==true)
return a>b;
else
return a<b;
}
private:
bool flag;
};
int main()
{
int arr[7]={
3,5,1,5,2,6,8};
vector<int> vi(arr,arr+6);
sort(vi.begin(),vi.end(),Compare(false));//升序降序由我决定
for(auto itr=vi.begin();itr!=vi.end();++itr)
{
cout<<*itr<<endl;
}
return 0;
}