STL17-函数对象

仿函数:

#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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/567912.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数据库操作php,一个数据库操作PHP类

/** Author 墨龙* Time 2010年12月2日 15:50:35*/$db new mysql($db_host,$db_user,$db_password,$db_table,$db_conn,$pre,$coding);class mysql{private $db_host;private $db_user;private $db_password;private $db_table;private $db_conn; //数据库连接标识;private $re…

STL18常用算法

#include<iostream> #include<algorithm> #include<vector> using namespace std; //transform 将一个容器中的元素搬运在另一个容器中 #if 0 //错误 struct PrintVector {void operator()(int v) {cout << v << " ";} }; void test0…

eclipse java ui,Eclipse Forms筹划漂亮UI之高级组件[Java编程]

赞助商链接本文“Eclipse Forms筹划漂亮UI之高级组件[Java编程]”是由七道奇为您精心收集&#xff0c;来源于网络转载&#xff0c;文章版权归文章作者所有&#xff0c;本站不对其观点以及内容做任何评价&#xff0c;请读者自行判断&#xff0c;以下是其具体内容&#xff1a;Ecl…

php中页面平滑回到顶部代码,原生JS实现平滑回到顶部组件

返回顶部组件是一种极其常见的网页功能&#xff0c;需求简单&#xff1a;页面滚动一定距离后&#xff0c;显示返回顶部的按钮&#xff0c;点击该按钮可以将滚动条滚回至页面开始的位置。实现思路也很容易&#xff0c;只要改变document.documentElement.scrollTop或document.bod…

C++基础01-C++对c的增强

所谓namespace&#xff0c;是指标识符的各种可见范围。C标准程序库中的所 有标识符都被定义于一个名为std的namespace中。 一 &#xff1a;<iostream>和<iostream.h>格式不一样&#xff0c;前者没有后缀&#xff0c;实际上&#xff0c; 在你的编译器include文件夹…

php环行队列实现,java数组实现队列及环形队列实现过程解析

这篇文章主要介绍了java数组实现队列及环形队列实现过程解析,文中通过示例代码介绍的非常详细&#xff0c;对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下代码内容ArrayQueue---用数组实现队列package com.structure;import java.util.Scanner;/*** auther:…

C++混淆点-static关键字理解

.先来介绍它的第一条也是最重要的一条&#xff1a;隐藏。&#xff08;static函数&#xff0c;static变量均可&#xff09; 当同时编译多个文件时&#xff0c;所有未加static前缀的全局变量和函数都具有全局可见性。 举例来说明。同时编译两个源文件&#xff0c;一个是a.c&…

php分页代码 页数太多,php分页函数示例代码分享

一例php分页函数代码。分享一例php分页函数代码&#xff0c;用此函数实现分页代码很不错。代码&#xff0c;php分页函数。/** 使用方法&#xff1a;require_once(mypage.php);$resultmysql_query("select * from mytable", $myconn);$totalmysql_num_rows($result); …

C++基础02-C++对c的拓展

变量名实质上是一段连续存储空间的别名&#xff0c;是一个标号(门牌号) 通过变量来申请并命名内存空间. 通过变量的名字可以使用存储空间. 变量名&#xff0c;本身是一段内存的引用&#xff0c;即别名(alias). 引用可以看作一个已定义变量的别名。 引用的语法&#xff…

matlab在电力系统故障的应用,MATLAB在电力系统故障分析中的应用

第5章MATLAB在电力系统故障分析中的仿真实例 5 1无穷大功率电源供电系统三相短路仿真5 2同步发电机突然短路的暂态过程仿真5 3小电流接地系统单相故障 5 1无穷大功率电源供电系统三相短路仿真 5 1 1无穷大功率电源供电系统三相短路的暂态过程5 1 2无穷大功率电源供电系统仿真模…

C++基础03-C++对c的拓展-函数

一、内联函数 c 语言中有宏函数的概念。宏函数的特点是内嵌到调用代码中去,避免了函数调用 的开销。但是由于宏函数的处理发生在预处理阶段,缺失了语法检测 和有可能带来的语 意差错。 特点&#xff1a; 1&#xff09;内联函数声明时inline关键字必须和函数定义结合在一起&a…

php小程序onload,微信小程序 loading 组件实例详解

这篇文章主要介绍了微信小程序 loading 组件实例详解的相关资料,需要的朋友可以参考下loading通常使用在请求网络数据时的一种方式&#xff0c;通过hidden属性设置显示与否主要属性&#xff1a;wxml显示loading正在加载jsPage({data:{// text:"这是一个页面"hiddenLo…

机器学习之实验过程02

机器学习之实验过程-数据清理 from sklearn.model_selection import train_test_split from sklearn.metrics import mean_squared_errordata_path = /home/py/Work/机器学习/labs/data/Feedback.csv df = pd.read_csv(data_path) df.head() print (df.tail()) rename_pai…

C++基础04-类基础

一、类和对象 面向对象三大特点&#xff1a;封装、继承、多态。 struct 中所有行为和属性都是 public 的(默认)。C中的 class 可以指定行为和属性的访问方式。 封装,可以达到,对内开放数据,对外屏蔽数据,对外提供接口。达到了信息隐蔽的功能。 class 封装的本质,在于将数…

matlab 矩阵位移法编程 结构力学,matlab 矩阵位移法编程 结构力学

矩阵位移法编程大作业(091210211)一、编制原理本程序的原理是基于结构力学矩阵位移法原理&#xff0c;以结构结点位移作基本未知量&#xff0c;将要分析的结构拆成已知节点力—结点力位移关系的单跨梁集合&#xff0c;通过强令结构发生待定的基本未知位移&#xff0c;在各个单跨…

C++混淆点-构造函数参数

#include<iostream> using namespace std;class Test { public://Test(int x, int y) { //或者将形参名不要等于数据成员名// //x x; //自赋值 错误// //y y;// this->x x; //正确 形参a屏蔽了成员变量a&#xff0c;所以必须用this指针索引&#xff0c;这样程序直…

php 静态页面模板类,dedetag.class.php 静态模板类

类文件include/dedetag.class.php这个文件是dedecms V5.3及之前版本使用的主要的模板类&#xff0c;它是解析式模板类&#xff0c;并非编译式的(区别是前者通过获得标签位置进行内容替换&#xff0c;后者是直接解析式PHP代码&#xff0c;二次执行)一、模板语法织梦模板引擎是一…

C/C++混淆点-字符串指针

c语言中没有字符串的类型&#xff0c; 所以对字符串操作&#xff0c;有两种形式:可以用字符指针&#xff0c;或者字符串数组&#xff08;这里的指针变量c, 系统会为其重新分配内存) &#xff08;1&#xff09;用字符数组存放一个字符串 char string[]"Linux C"; pri…

python 文件上传下载,python实现上传下载文件功能

最近刚学python,遇到上传下载文件功能需求&#xff0c;记录下&#xff01;django web项目&#xff0c;前端上传控件用的是uploadify。文件上传 - 后台view 的 Python代码如下&#xff1a;csrf_exemptrequire_http_methods(["POST"])def uploadFiles(request):try:use…

C++基础05-类构造函数与析构函数

总结&#xff1a; 1、类对象的作用域为两个{}之间。在遇到}后开始执行析构函数 2、当没有任何显式的构造函数&#xff08;无参&#xff0c;有参&#xff0c;拷贝构造&#xff09;时&#xff0c;默认构造函数才会发挥作用 一旦提供显式的构造函数&#xff0c;默认构造函数不复…