C++primeer 6.7节练习(函数指针,函数指针形参)
来源:互联网 发布:java抽奖概率算法 编辑:程序博客网 时间:2024/05/22 15:58
6.54
#include <iostream> #include <string>#include <vector>using namespace std;/*函数指针*/int test(int a, int b);/*定义一个指向test函数类型的指针 pf*/int(*pf)(int, int); /*用decltype和typedef定义此函数类型指针*/typedef decltype(test) *pf1;/*或者这样写*/typedef int(*pf2)(int, int);/*定义一个vector对象,其元素是指向此函数类型的指针*/vector<pf1> Vec;/*或是这样*/vector<decltype(test) *> Vec1;
6.56
#include <iostream> #include <string>#include <vector>using namespace std;/*函数指针*/int Sum(int a, int b){return a + b;}int Dec(int a, int b){return a - b;}int Mul(int a, int b){return a * b;}int Div(int a, int b){return a / b;}/*定义一个指向test函数类型的指针 pf*/int(*pf)(int, int);/*用decltype和typedef定义此函数类型指针*/typedef decltype(Sum) *pf1;/*或者这样写*/typedef int(*pf2)(int, int);/*定义一个vector对象,其元素是指向此函数类型的指针*/vector<pf1> Vec{Sum,Dec,Mul,Div};/*或是这样*/vector<decltype(Sum) *> Vec1{Sum,Dec,Mul,Div};/*函数指针形参*/void Compute1(int j, int k, pf1 PF1){cout << PF1(j,k) << endl;}/*或者这样写*/void Compute2(int j, int k, int(*PF2)(int, int)){cout << PF2(j, k) << endl;}int main(){int A = 5, B = 10;cout << "采用Compute1" << endl;cout << "加减乘除结果为: " << endl;for (auto p : Vec)Compute1(A, B, p);cout << "采用Compute2" << endl;cout << "加减乘除结果为: " << endl;for (auto p1 : Vec)Compute2(A, B, p1);system("pause");return 0;}
阅读全文
0 0
- C++primeer 6.7节练习(函数指针,函数指针形参)
- C 函数指针练习
- c函数和指针练习
- 函数指针(C++)
- C指针函数 && 函数指针
- 指针函数练习
- 函数指针终极练习
- 指针函数小练习
- 函数指针练习
- 函数指针与指针函数练习
- C++----函数&函数指针
- C语言中的指针(指针数组 数组指针 函数指针 函数指针数组 指向函数指针数组的指针)
- C语言练习篇-8函数指针练习
- C程序设计语言--指针与数组(二) 指针数组 数组指针 函数指针 指针函数
- 二级指针和指针引用函数传参(C++)
- 【C和指针】函数指针
- 《c和指针》函数指针
- C 语言--函数指针形参
- servlet context 和 servlet config
- GitHub远程仓库的创建以及本地仓库与远程仓库同步(文字+图解)
- C++类和对象1--四个默认成员函数+运算符重载
- 前端工具——npm
- Konektatbed Electra 6.20 1CD
- C++primeer 6.7节练习(函数指针,函数指针形参)
- python ssl相关问题说明
- 用ELK导入历史log的正确姿势
- git使用
- 使用简单的方法发布自己的android开源库
- Linux CentOS 安装JDK
- PopupWindow小坑:无论偏移多大,不会跑出屏幕。 充满显示上方空间
- List报空指针异常
- Python中的string模块的学习