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

C++笔记之通用多态函数包装器std::function

C++笔记之通用多态函数包装器std::function

code review!

文章目录

  • C++笔记之通用多态函数包装器std::function
    • 1.存储自由函数,lambda,std::bind 调用的结果
    • 2.存储到成员的调用
    • 3.存储到函数对象
    • 四.基本语法
    • 五.使用std::function定义函数对象
    • 六.使用std::function结合Lambda表达式定义函数对象
    • 七.使用std::function实现回调机制——略,有专门新开笔记介绍。

1.存储自由函数,lambda,std::bind 调用的结果

在这里插入图片描述

代码

#include <functional>
#include <iostream>void print_num(int i) {std::cout << i << '\n';
}int main() {std::cout << "存储自由函数---1" << std::endl;std::function<void(int)> f_display = print_num;f_display(-9);std::cout << "存储 lambda--2" << std::endl;std::function<void()> f_display_42 = []() { print_num(42); };f_display_42();std::cout << "存储到 std::bind 调用的结果--3" << std::endl;std::function<void()> f_display_31337 = std::bind(print_num, 31337);f_display_31337();
}

运行:
存储自由函数—1
-9
存储 lambda–2
42
存储到 std::bind 调用的结果–3
31337

2.存储到成员的调用

在这里插入图片描述

代码

#include <functional>
#include <iostream>struct Foo {Foo(int num) : num_(num) {}void print_add(int i) const { std::cout << num_ + i << '\n'; }int num_;
};int main() {const Foo foo(314159);foo.print_add(1);std::cout << "存储到成员函数的调用---1" << std::endl;std::function<void(const Foo &, int)> f_add_display = &Foo::print_add;f_add_display(foo, 1);f_add_display(314159, 1);std::cout << "存储到数据成员访问器的调用---2" << std::endl;std::function<int(Foo const &)> f_num = &Foo::num_;std::cout << "num_: " << f_num(foo) << '\n';std::cout << "存储到成员函数及对象的调用---3" << std::endl;using std::placeholders::_1;std::function<void(int)> f_add_display2 = std::bind(&Foo::print_add, foo, _1);f_add_display2(2);
}

运行:
314160
存储到成员函数的调用—1
314160
314160
存储到数据成员访问器的调用—2
num_: 314159
存储到成员函数及对象的调用—3
314161

3.存储到函数对象

在这里插入图片描述

#include <functional>
#include <iostream>struct PrintNum {void operator()(int i) const {std::cout << i << '\n';}
};int main() {// 存储到函数对象的调用std::function<void(int)> f_display_obj = PrintNum();f_display_obj(18);auto factorial = [](int n) {// 存储 lambda 对象以模拟“递归 lambda ”,注意额外开销std::function<int(int)> fac = [&](int n) { return (n < 2) ? 1 : n * fac(n - 1); };// note that "auto fac = [&](int n){...};" does not work in recursive callsreturn fac(n);};for (int i{5}; i != 8; ++i) {std::cout << i << "! = " << factorial(i) << ";  ";}
}

代码:
18
5! = 120; 6! = 720; 7! = 5040;

四.基本语法

在这里插入图片描述

五.使用std::function定义函数对象

在这里插入图片描述

六.使用std::function结合Lambda表达式定义函数对象

在这里插入图片描述

七.使用std::function实现回调机制——略,有专门新开笔记介绍。

http://www.lryc.cn/news/193035.html

相关文章:

  • Linux命令(92)之passwd
  • 光电柴微电网日前调度报告
  • Godot 单元测试
  • 2.9 深入GPU硬件架构及运行机制
  • 【苍穹外卖 | 项目日记】第四天
  • 零代码编程:用ChatGPT批量采集bookroo网页上的英文书目列表
  • 7.定时器
  • 计算机网络 | 网络层
  • 21GA-ELM,遗传算法优化ELM预测,并和优化前后以及真实数值进行对比,确定结果,基于MATLAB平台,程序已经调通,可以直接运行,需要直接拍下。
  • 287_C++_TaskQueue管理任务队列和定时器(头文件.h)
  • Hadoop+Zookeeper+HA错题总结(一)
  • React高级特性之context
  • 【OS】操作系统课程笔记 第五章 并发性——互斥、同步和通信
  • RabbitMQ概述原理
  • 8.Covector Transformation Rules
  • RustDay04------Exercise[21-30]
  • OpenAI科学家谈GPT-4的潜力与挑战
  • Java电子病历编辑器项目源码 采用B/S(Browser/Server)架构
  • 使用 AWS DataSync 进行跨区域 AWS EFS 数据传输
  • 设计模式~解释器模式(Interpreter)-19
  • 对象混入的实现方式
  • Mac 远程 Ubuntu
  • 黑豹程序员-h5前端录音、播放
  • Leetcode622.设计循环队列
  • 二十二、【形状工具组】
  • 设计模式~迭代器模式(Iterator)-20
  • 亳州市的自然风光与旅游资源:欣赏安徽省中部的壮丽景色
  • windows安装nvm以及解决yarn问题
  • 【TA 挖坑04】薄膜干涉 镭射材质 matcap
  • OpenCV13-图像噪声:椒盐噪声和高斯噪声