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

C++笔记之从数组指针到函数数组指针(使用using name和std::function)

C++笔记之从数组指针到函数数组指针(使用using name和std::function)

参考笔记:
C++之指针探究(三):指针数组和数组指针
C++之指针探究(十三):函数指针数组
C++之指针探究(二):一级指针和一维数组
C++之指针探究(十一):函数名的本质和函数指针
C++笔记之从使用函数指针和typedef到使用std::function和using
C++之指针探究(八):指针函数和函数指针

code review!

文章目录

  • C++笔记之从数组指针到函数数组指针(使用using name和std::function)
    • 1.指向数组的指针
    • 2.指向动态数组的指针
    • 3.函数指针数组和std::function、using结合使用的例程
      • 形式一:MathFunction mathFunctions[] = {add, subtract, multiply, divide};
      • 形式二:MathFunction *mathFunctions[] = {add, subtract, multiply, divide};
      • 形式三:MathFunction *mathFunctions = new MathFunction[4];
    • 附代码

1.指向数组的指针

在这里插入图片描述

2.指向动态数组的指针

在这里插入图片描述

3.函数指针数组和std::function、using结合使用的例程

形式一:MathFunction mathFunctions[] = {add, subtract, multiply, divide};

在这里插入图片描述

形式二:MathFunction *mathFunctions[] = {add, subtract, multiply, divide};

在这里插入图片描述

形式三:MathFunction *mathFunctions = new MathFunction[4];

在这里插入图片描述

附代码

形式一:

#include <iostream>
#include <functional>// 定义不同类型的函数
int add(int a, int b) {return a + b;
}int subtract(int a, int b) {return a - b;
}double multiply(double a, double b) {return a * b;
}double divide(double a, double b) {return a / b;
}// 创建函数指针数组类型
using MathFunction = std::function<double(double, double)>;int main() {// 创建函数指针数组MathFunction mathFunctions[] = {add, subtract, multiply, divide};// 使用函数指针数组调用不同函数double x = 10.0, y = 5.0;for (const MathFunction &func : mathFunctions) {std::cout << func(x, y) << std::endl;}return 0;
}

形式二:

#include <iostream>
#include <functional>// 定义不同类型的函数
int add(int a, int b) {return a + b;
}int subtract(int a, int b) {return a - b;
}double multiply(double a, double b) {return a * b;
}double divide(double a, double b) {return a / b;
}// 创建函数指针数组类型
using MathFunction = std::function<double(double, double)>;int main() {// 创建指针数组并初始化MathFunction *mathFunctions[] = {add, subtract, multiply, divide};// 使用指针数组调用不同函数double x = 10.0, y = 5.0;for (MathFunction *func : mathFunctions) {std::cout << (*func)(x, y) << std::endl;}return 0;
}

形式三:

#include <iostream>
#include <functional>// 定义不同类型的函数
int add(int a, int b) {return a + b;
}int subtract(int a, int b) {return a - b;
}double multiply(double a, double b) {return a * b;
}double divide(double a, double b) {return a / b;
}// 创建函数指针数组类型
using MathFunction = std::function<double(double, double)>;int main() {// 创建指针数组并初始化MathFunction *mathFunctions = new MathFunction[4];mathFunctions[0] = add;mathFunctions[1] = subtract;mathFunctions[2] = multiply;mathFunctions[3] = divide;// 使用指针数组调用不同函数double x = 10.0, y = 5.0;for (int i = 0; i < 4; ++i) {std::cout << mathFunctions[i](x, y) << std::endl;}// 释放内存delete[] mathFunctions;return 0;
}
http://www.lryc.cn/news/111537.html

相关文章:

  • 【数据结构】常见的排序算法
  • CentOS 安装 Jenkins
  • 前端如何设置表格边框样式和单元格间距?
  • Ubuntu 22.04安装搜狗输入法
  • 【C++】初阶 --- 内联函数(inline)
  • VGGNet剪枝实战:使用VGGNet训练、稀疏训练、剪枝、微调等,剪枝出只有3M的模型
  • 【iOS】GCD深入学习
  • Webpack开启本地服务器;HMR热模块替换;devServer配置;开发与生成环境的区分与配置
  • opencv 31-图像平滑处理-方框滤波cv2.boxFilter()
  • Kubernetes关于cpu资源分配的设计
  • Flink读取mysql数据库(java)
  • 小程序学习(五):WXSS模板语法
  • 注解 @JsonFormat 与 @DateTimeFormat 的使用
  • Python实现决策树算法:完整源码逐行解析
  • Linux文本三剑客---grep、sed、awk
  • 局域网VoIP网络电话测试
  • el-table 去掉边框(修改颜色)
  • redis与MongoDB的区别
  • CSS设置高度
  • 开源免费用|Apache Doris 2.0 推出跨集群数据复制功能
  • 【docker】docker-compose服务编排
  • EdgeBox_tx1_A200 PyTorch v1.9.0 环境部署
  • 【雕爷学编程】MicroPython动手做(33)——物联网之天气预报
  • 分库分表之基于Shardingjdbc+docker+mysql主从架构实现读写分离 (三)
  • 探秘企业DevOps一体化平台建设终极形态丨IDCF
  • 百度智能创做AI平台
  • Python 开发工具 Pycharm —— 使用技巧Lv.1
  • zookeeper --- 高级篇
  • TypeScript【enum 枚举】
  • SpringBoot项目增加logback日志文件