#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*编写一些形参为int,且返回类型也是int的函数,然后声明一个vector对象,令其元素指向该函数的指针*/
//加减乘除的函数
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { return b != 0 ? a / b : 0; }
int main()
{
//定义vector 容器对象
//vector的对象是指向该函数的指针
vector<int(*)(int, int)> vf{ add, subtract, multiply, divide };
//使用范围for 循环来依次调用这几个函数。
for (const auto &e : vf) cout << e(4, 2) << endl;
return 0;
}
04-29
2020