当前位置: 首页 > news >正文

函数对象(仿函数)适配器

文章目录

  • 函数对象(仿函数)适配器
    • 函数适配器
    • 作用
    • bind1st和bind2nd区别?
    • 示例
    • 取反适配器
      • 一元取反适配器 not1
      • 二元取反适配器 not2
    • 函数指针适配器 ptr_fun
    • 成员函数适配器 mem_fun_ref 和 mem_fun

函数对象(仿函数)适配器

函数对象绑定参数 ,将多个参数绑定为一个参数

函数适配器

作用

函数对象绑定参数 ,将多个参数绑定为一个参数

bind1st和bind2nd区别?

  • bind1st : 将参数绑定为函数对象的第一个参数

  • bind2nd : 将参数绑定为函数对象的第二个参数

  • bind1st bind2nd将二元函数对象转为一元函数对象

示例

#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;/*1、绑定 bind2nd2、继承 binary_function3、加const*/
class MyPrint :public binary_function<int,int,void>  //这里继承binary_function<参数类型,参数类型,返回值类型>
{
public:void operator()(int val,int start)const //这里加const{cout << val + start << endl;}
};void test01()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}cout << "请输入起始累加值:" << endl;int start = 0;cin >> start;for_each(v.begin(), v.end(), bind2nd(MyPrint(), start));  //bind2nd//for_each(v.begin(), v.end(), bind1st(MyPrint(), start));}

取反适配器

一元取反适配器 not1

#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;class GreaterThenFive : public unary_function<int, bool>
{
public:bool operator()(int val) const{return val > 5;}
};//取反适配器
void test()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}//一元取反//vector<int>::iterator itPos = find_if(v.begin(), v.end(), not1(GreaterThenFive()));//优化后的语法vector<int>::iterator itPos = find_if(v.begin(), v.end(), not1(bind2nd(greater<int>(), 5)));if (itPos != v.end()){cout << "找到了元素:" << *itPos << endl;}
}

二元取反适配器 not2

#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;void test()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}sort(v.begin(), v.end(), not2(less<int>()));for_each(v.begin(), v.end(), [](int val) {cout << val << " ";});cout << endl;
}

函数指针适配器 ptr_fun

#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
using namespace std;//函数指针适配器
void myPrint(int val, int start)
{cout << val + start << endl;
}
void test()
{vector<int>v;for (int i = 0; i < 10; ++i){v.push_back(i);}cout << "请输入起始累加值:" << endl;int start = 0;cin >> start;//这里我们提供的是回调函数,所以需要将函数指针适配成函数对象 ptr_fun(myPrint)for_each(v.begin(), v.end(), bind2nd(ptr_fun(myPrint),start));
}

成员函数适配器 mem_fun_ref 和 mem_fun

  • 如果容器中存放的是对象实体,那么用mem_fun_ref
  • 如果容器存放的是对象指针, 那么用mem_fun
#include<iostream>
#include<vector>
#include<algorithm>
#include <functional>
#include<string>
using namespace std;class Person
{
public:Person(string name, int age) : m_name(name), m_age(age){}void ShowPerson(){cout << "name:" << m_name << " age:" << m_age << endl;}void AddAge(){m_age++;}string m_name;int m_age;
};//成员函数适配器
void test01()
{//如果容器中存放的是对象实体,那么用mem_fun_refvector<Person>v;v.reserve(3);Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);v.push_back(p1);v.push_back(p2);v.push_back(p3);//成员函数适配器 mem_fun_ref(成员函数的入口地址)for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));for_each(v.begin(), v.end(), mem_fun_ref(&Person::AddAge));for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));}void test02()
{//如果容器存放的是对象指针,  那么用mem_funvector<Person*>v;v.reserve(3);Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);//成员函数适配器 mem_fun(成员函数的入口地址)for_each(v.begin(), v.end(), mem_fun(&Person::ShowPerson));for_each(v.begin(), v.end(), mem_fun(&Person::AddAge));for_each(v.begin(), v.end(), mem_fun(&Person::ShowPerson));
}
http://www.dtcms.com/a/330097.html

相关文章:

  • 《量子雷达》第2章 从量子信息到量子雷达 预习2025.8.13
  • 工业视觉检测中的常见的四种打光方式
  • Java 导出word 实现表格内插入图表(柱状图、折线图、饼状图)--可编辑数据
  • java反射与泛型的简单知识和应用
  • 【KO】Android 网络相关面试题
  • 326. 3 的幂
  • 不用费心备份操作的实验记录本
  • VUE基础笔记
  • 【AI学习100天】Day07 加入AI社区,通往AGI之路
  • C# 反射和特性(获取Type对象)
  • 【C#】利用数组实现大数数据结构
  • Spring Cloud系列— Alibaba Sentinel限流
  • Pycharm现有conda环境有对应env,但是添加后没反应
  • 《人形机器人的觉醒:技术革命与碳基未来》——生物混合肌肉:技术原理和进展、比较优势和不足、材料技术要求及材料限制
  • 递归函数与 lambda 函数:用法详解与实践
  • Synchronized锁的使用方式
  • three.js学习记录(鼠标控制)
  • Linux 计划任务
  • 【web站点安全开发】任务3:网页开发的骨架HTML与美容术CSS
  • STM32学习笔记10—DMA
  • JSON索引香港VPS:高效数据处理的完美解决方案
  • JDK17下载与安装图文教程(保姆级教程)
  • 《汇编语言:基于X86处理器》第13章 复习题和编程练习
  • VerIF
  • 【R语言】RStudio 中的 Source on Save、Run、Source 辨析
  • [系统架构设计师]系统架构基础知识(一)
  • MySQL表约束
  • 关于大学计算机专业的课程的一些看法
  • windows通过共享网络上网
  • JavaWeb之响应