标签:仿函数
1、仿函数
本质:是一个对象,用起来像函数;
原因:在类内对()进行了重载;
2、仿函数和回调函数的区别
(1)、代码如下:
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
//函数对象:类重载了();
//函数对象和函数是很类似的;
template<typename Type>
class ShowElem{
public:
ShowElem(){
n = 0;
}
void operator()(Type &t){ //重载了(),此时可以叫做函数对象;
n++;
cout<<t<<endl;
}
void printN(){
cout<<"n:"<<n<<endl;
}
protected:
private:
int n;
};
void main01(){
int a = 10;
ShowElem<int> showElem;
showElem(a); //函数对象的()的执行,很像一个函数;所以又叫做仿函数;
//函数对象可以跟函数的调用一样,直接把对象当作函数名称来使用!!!
}
//函数对象和普通函数的区别;
template<typename Type> //函数模版
void FuncShowElem(Type &t){
cout<<t<<endl;
}
void FuncShowElem2(int &t){ //普通函数
cout<<t<<endl;
}
void main02(){
vector<int> v1;
v1.push_back(1);
v1.push_back(3);
v1.push_back(5);
for_each(v1.begin(), v1.end(), ShowElem<int>()); //匿名函数对象 匿名仿函数;
cout<<endl;
for_each(v1.begin(), v1.end(), FuncShowElem2); //通过回调函数;谁使用for_each()
}
int main(void){
main01();
main02();
return 0;
}(2)、运行结果:
本文出自 “wait0804” 博客,请务必保留此出处http://wait0804.blog.51cto.com/11586096/1875944
标签:仿函数
原文地址:http://wait0804.blog.51cto.com/11586096/1875944