相关博文:C++新特性探究(九):functor仿函数

Functor 对象模拟函数

  把类对象,像函数名一样使用。
  仿函数(functor),就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator( ),这个类就有了类似函数的行为,就是一个仿函数类了。

operator( )语法格式:

例1:

附上例代码:

//小问学编程
#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

附上例代码:

//小问学编程
#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;
}

补充例:


附补充例代码:

//小问学编程
#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;
}

例3:std::sort回调函数

第一种

第二种

第三种

例4:functor的优势在于,是对象形式,可以携带更多的信息,用于做出判断。比如,我们可以在对象初始化的时候,传入参数来决定状态,而不用去修改源代码。

该程序中携带信息,升序降序由我决定

附例3,4代码:

第一种
//小问学编程
#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;elsereturn 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;
}

C++新特性探究(9.1):functor仿函数探究相关推荐

  1. C++新特性探究(九):functor仿函数

    相关博文:C++新特性探究(9.1):functor仿函数探究 仿函数技术难度不高,但对菜鸟来说侮辱性极强! 一. operator( )   重载了operator()的类的对象,在使用中,语法类似 ...

  2. C++新特性探究(十):Lambda

    一. 匿名函数   简短函数,就地书写,调用.即Lambda存在的意义,常用于取代作回调用的简短函数指针与仿函数.   就地书写,因只有函数体,即无函数名,也称匿名函数. 格式 最小的 Lambda: ...

  3. C++新特性探究(十四):function

    相关博文:C++头文件<functional>和bind.placeholders占位符使用简单例子 相关博文:<Essential C++>笔记之设计一个泛型算法(二) 相关 ...

  4. Java 8新特性探究(二)深入解析默认方法

    转载自 Java 8新特性探究(二)深入解析默认方法 什么是默认方法,为什么要有默认方法 简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法.只需在方法名前面加个default关键字即可. ...

  5. C++新特性探究(13.6):右值引用再探究

    相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...

  6. C++新特性探究(13.5):右值引用

    相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...

  7. C++新特性探究(十六):move constructor移动构造

    相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...

  8. C++新特性探究(十五):bind

    相关博文:C++新特性探究(十四):function 相关博文:C++头文件<functional>和bind.placeholders占位符使用简单例子 相关博文:<Essenti ...

  9. C++新特性探究(十三):右值引用(r-value ref)探究

    相关博文: C++新特性探究(十三):右值引用(r-value ref)&&探究 C++新特性探究(十六):move constructor移动构造 C++新特性探究(13.5):右值 ...

最新文章

  1. 电大计算机基础知识考题,2016年电大计算机基础知识模拟试题.doc
  2. 【转载】python 编码问题 u'汉字'
  3. 女人,就是不适合做IT!
  4. 计算机语言无限循环,求大神帮我看看为什么我的子程序无限循环无法使用F8停止...
  5. 20145302张薇《Java程序设计》第十周学习总结
  6. 一文掌握Python集合的语法与应用
  7. java中的关键字transient说明
  8. Axis2创建WebService实例
  9. 史上最全面的深度学习硬件指南
  10. Design Pattern 设计模式【观察者】
  11. JavaScript 教程
  12. 硬链接(hard link)和符号连接(symbolic link)的区别
  13. 年轻人先实现社会价值,再去实现人生价值
  14. 智能厨房监控系统设计
  15. 为了防止火灾发生,安科瑞余压监控系统在某高层住宅的应用方案
  16. Python量化:评估投资组合的收益率和风险
  17. 解读全球免费化:是众望所归还是坑死不赔
  18. 手机app抓包,无视SSLPinning
  19. 【java毕业设计】基于javaEE+原生Servlet+MySql的企业财务管理系统设计与实现(毕业论文+程序源码)——企业财务管理系统
  20. centos7安装luarocks

热门文章

  1. HashMap + 软引用进行缓存
  2. 基于JAVA+SpringMVC+Mybatis+MYSQL的学校教务查询系统
  3. android对错图标,Android Studio Gradle图标错误,清单合并
  4. ASP.NET Core学习——5
  5. 德国商业经济金融发展史
  6. bzoj千题计划277:bzoj4513: [Sdoi2016]储能表
  7. 整合Flask中的目录结构
  8. Java 使用SAX解析XML文档
  9. windows 批处理设置环境变量
  10. 【练习】删除表中的分区