仿函数:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//仿函数(函数对象)重载“()”操作符 使类对象可以像函数那样调用
//仿函数是一个类,不是一个函数
//函数对象可以像普通函数一样调用
//函数对象可以像普通函数那样接收参数
//函数对象超出了函数的概念,函数对象可以保存函数调用的状态struct MyPrint {  void operator()(int val) {cout << val;}
};
void test01() {MyPrint print;print(10);
}
#if 0
int num = 0;  //在开发中,避免使用全局变量 多个线程对变量处理 加锁解锁
void MyPrint02(int val) {cout << val;num++;
}
#endif
//避免全局变量的方法
struct MyPrint02 {MyPrint02(){mNum = 0;}void operator()(int val) {mNum++;cout << val<<endl;}
public:int mNum;
};void test02() {MyPrint02 print;print(10);print(20);cout << print.mNum << endl;
}
void test03() {vector<int> v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);MyPrint02 print;MyPrint02 print2=for_each(v.begin(), v.end(), print);cout << "print调用次数:"  << print.mNum << endl;  //0cout << "print2调用次数:" <<print2.mNum << endl;  //4}
#if 1
int main() {cout << "test01" << endl;test01();cout << endl<<"test02" << endl;test02();cout <<endl<< "test03" << endl;test03();return 0;
}
#endif

#include<iostream>
#include<functional>
using namespace std;void test01() {//使用内建函数对象声明一个对象plus<int> myclass;cout<<myclass(10, 20)<<endl;//使用匿名临时对象cout << plus<int>()(5, 6) << endl;
}
#if 1
int main() {test01() ;return 0;
}
#endif

#include<iostream>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
//仿函数适配器 bindlst bind2nd 绑定适配器
struct MyPrint {void operator()(int v) {cout << v << " ";}
};
struct MyPrint2 :public binary_function<int,int,void>{  //第一个参数类型 第二个参数类型 返回值类型void operator()(int v,int value) const{cout << "v:" << v <<" " <<"val:" << value << endl;//cout << v+value << " ";}
};
void test01() {vector<int> v;for (int i = 0; i < 10; i++) {v.push_back(i);}MyPrint print;for_each(v.begin(), v.end(), print);cout << endl;for_each(v.begin(), v.end(), MyPrint());cout << endl;//如何给仿函数传入两个参数//for_each(v.begin(), v.end(), MyPrint2(100));  错误int addNum = 100;cout << "------------bind2nd-----------------" << endl;for_each(v.begin(), v.end(), bind2nd(MyPrint2(),addNum));cout << "------------bind1st-----------------" << endl;for_each(v.begin(), v.end(), bind1st(MyPrint2(), addNum));//绑定适配器 将一个二元函数对象转换为一元函数对象//bindlst bind2nd 区别//bindlst将addNum绑定为函数对象的第一个参数//bind2nd将addNum绑定为函数对象的第二个参数
}
struct MyCompare01 {bool operator()(int v1, int v2){return v1 > v2;}
};
struct MyCompare02 :public binary_function<int,int,bool>{bool operator()(int v1, int v2) const{return v1 > v2;}
};
struct MyGreater5 {bool operator()(int v) {return v > 5;}
};
struct MyGreater55:public unary_function<int,bool> {bool operator()(int v) const {return v > 5;}
};
struct MyPrint02 {void operator()(int v) {cout << v << " ";}
};
//仿函数适配器 not1 not2 取反适配器
void test02() {vector<int> v;for (int i = 0; i < 10; i++) {/*v.push_back(rand()%100+10);*/v.push_back(i);}for_each(v.begin(), v.end(), MyPrint02());cout <<endl<< "-------排序后-------"<<endl;sort(v.begin(), v.end(), MyCompare01());for_each(v.begin(), v.end(), MyPrint02()); cout<<endl << "-------取反排序后-------" << endl;sort(v.begin(), v.end(), not2(MyCompare02()));for_each(v.begin(), v.end(), MyPrint02());//not1 和 not2 区别//如果对二元谓词取反 用 not2//如果对一元谓词取反 用 not1cout <<endl<< "not1" << endl;vector<int>::iterator ret=find_if(v.begin(), v.end(), MyGreater5());cout << *ret << endl;vector<int>::iterator ret1 = find_if(v.begin(), v.end(),not1( MyGreater55()));if (ret1 == v.end()) {cout << "没有找到" << endl;}elsecout << *ret1 << endl;}
void MyPrint033(int val1,int val2) {cout << val1 << " "<<val2<<" "<<endl;
}
void MyPrint03(int val) {cout << val << " ";
}
//仿函数适配器 ptr_fun 把普通函数 转成 函数对象
void test03() {vector<int> v;for (int i = 0; i < 10; i++) {/*v.push_back(rand()%100+10);*/v.push_back(i);}for_each(v.begin(), v.end(), MyPrint03);  //此处函数没有参数cout << endl;//把普通函数 转成 函数对象for_each(v.begin(), v.end(), bind2nd(ptr_fun(MyPrint033),10));  //此处函数没有参数
}
//仿函数适配器 mem_fun mem_fun_ref
class Person {
public:/*Person(int age, int id) {错误age = age;id = id;}*/   /*Person(int age, int id) { 正确this->age = age;this->id = id;}*/Person(int age, int id) :age(age), id(id) {}  //正确void show() {cout << "age:" << age << "id:" << id << endl;}
public:int age;int id;
};
struct PrintPerson {void operator()(Person p) {p.show();}
};
void test04() {//如果容器中存放的是对象或者对象指针,我们for_each打印时,调用类//自己提供的打印函数vector<Person> vp;Person p1(10, 20);Person p2(20, 30);Person p3(30, 40);Person p4(40, 50);vp.push_back(p1);vp.push_back(p2);vp.push_back(p3);vp.push_back(p4);for_each(vp.begin(), vp.end(), PrintPerson());cout << "-----mem_fun_ref使用--------" << endl;for_each(vp.begin(), vp.end(), mem_fun_ref(&Person::show));vector<Person*> vpp;vpp.push_back(&p1);vpp.push_back(&p2);vpp.push_back(&p3);vpp.push_back(&p4);cout << "-----mem_fun使用--------" << endl;for_each(vpp.begin(), vpp.end(), mem_fun(&Person::show));//mem_fun_ref mem_fun区别?//如果存放的是对象指针用mem_fun//如果存放的是对象,使用mem_fun_ref}
#if 1
int main() {cout << "test01" << endl;test01();cout << endl<<"test02" << endl;test02();cout <<endl<< "test03" << endl;test03();cout << endl << "test04" << endl;test04();
}
#endif

STL17-函数对象相关推荐

  1. 函数对象、 函数对象与容器、函数对象与算法

    一.函数对象 1.函数对象(function object)也称为仿函数(functor) 2.一个行为类似函数的对象,它可以没有参数,也可以带有若干参数. 3.任何重载了调用运算符operator( ...

  2. R语言match函数对象之间的匹配实战

    R语言match函数对象之间的匹配实战 目录 R语言match函数对象之间的匹配实战 #基本语法 #match函数 # 匹配两个向量

  3. python函数装饰函数_Python精进-装饰器与函数对象

    本文为<爬着学Python>系列第四篇文章. 从本篇开始,本专栏在顺序更新的基础上,会有不规则的更新. 在Python的学习与运用中,我们迟早会遇到装饰器,这个概念对于初识装饰器的新手来说 ...

  4. 函数对象,名称空间与作用域,闭包,装饰器

    函数是第一类对象即可以被当做数据取使用 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 1.可以被引用 先看变量可以怎么玩 age=18 x=age 把 ...

  5. python命名空间和闭包_Python函数基础实例详解【函数嵌套,命名空间,函数对象,闭包函数等】...

    本文实例讲述了Python函数基础用法.分享给大家供大家参考,具体如下: 一.什么是命名关键字参数? 格式: 在*后面参数都是命名关键字参数. 特点: 1.约束函数的调用者必须按照Kye=value的 ...

  6. python编写函数isodd(x)_python函数对象

    适用于python 2.x版本 1. lambda函数 1 func = lambda x, y : x + y2 print func(2, 4) lambda生成一个函数对象,参数是x,y, 返回 ...

  7. 通过 “函数对象”看javascript函数

    #前言 本文主要阐述一些JavaScript行为的深层原因.很多东西都是主观臆测结合编程经验推测出来的. 如有谬误请包涵,也可以联系作者(qq:504451056,email:504451056@qq ...

  8. C++模板学习之函数对象之谓词

    函数对象是用对象来表示的函数: 可以执行operator()的对象都叫做函数对象. 谓词是那些返回bool,operator()操作的函数对象. 考虑如何对一个序列求和: 函数对象的优势在于可以将参数 ...

  9. python 返回函数对象_Python—函数对象与闭包

    一 函数对象 函数对象指的是函数可以被当做"数据"来处理,具体可以分为四个方面的使用. 1.1 函数可以被引用 def index(): print('from index') a ...

  10. python装饰器函数-Python精进-装饰器与函数对象

    本文为<爬着学Python>系列第四篇文章. 从本篇开始,本专栏在顺序更新的基础上,会有不规则的更新. 在Python的学习与运用中,我们迟早会遇到装饰器,这个概念对于初识装饰器的新手来说 ...

最新文章

  1. 常见几种浏览器兼容性问题与解决方案
  2. 【opencv】20.直方图均衡化的数学原理
  3. MySQL ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)的真正原因...
  4. 多个php版本的composer使用
  5. Android复盘OkHttp HttpLoggingInterceptor造成的OOM
  6. VTK:图像高斯平滑用法实战
  7. SAP UI5 应用开发教程之三十五 - 如何把本地开发的 SAP UI5 应用部署到 ABAP 服务器上
  8. j2ee不同数据库的驱动和URL写法
  9. Netty文章目录汇总
  10. wecp 启动 php,WEPE工具箱VIP版及网络版合盘20171102
  11. Java物联网平台后端架构构思设计
  12. window10 删除桌面删除不掉的ie图标(快捷方式)
  13. 两个重要极限及其推导过程
  14. 大学课设之Mysql图书管理设计
  15. matlab拷贝不进u盘,Mac无法拷贝文件到U盘怎么办
  16. Unity UI 动画 工具
  17. 大数据应用技术实验报告五 NoSQL
  18. 申请工作居住证办事指南
  19. Visual Studio Code (echarts)数据可视化,设置数据缩放区间为20到40,设置标题字体大小,颜色以及是否加粗,设置柱状图颜色
  20. MySQL数据库服务的简单开启和关闭

热门文章

  1. 部署到gcp_剖析大数据公司为什么选择 GCP?
  2. python写windows程序_【Python学习】Python 写Windows Service服务程序
  3. ladder怎么读_ladder 是什么意思_ladder 的翻译_音标_读音_用法_例句_爱词霸在线词典...
  4. php ai库,收藏 | 深度学习框架、AI库、ML库、NLP库、CV库汇总!
  5. python函数应用_python 函数应用
  6. dbeaver无法修改表数据_隐藏彩蛋:你知道python有一个内置的数据库吗?
  7. android运营商获取本机号码_一键登录已成大势所趋,Android端操作指南来啦!
  8. 交易系统如何确保账簿100%准确
  9. Oracle查询表|注释|字段|字段注释
  10. 一篇博客读懂设计模式之---委派模式